【python】pandas库中的merge方法连接两个表后输出为空

nixing123 2016-09-23 08:05:15
import pandas as pd

#读取数据,更换列表名
data = pd.read_table( 'u.data' )
datafeilds = ['user id', 'item id', 'rating', 'timestamp']
data.columns = datafeilds

#将文件中需要的数据分别存入两个列表
user_id = []
gender = []
userfile = open('u.user')
contents = userfile.readlines()
userfile.close()
for i in range(0,len(contents)):
line = contents[i].split('|')
user_id.append( line[0] )
gender.append( line[2] )

#存入数据,为dataframe类型
dictuser = {'user id':user_id, 'gender':gender}
user = pd.DataFrame(dictuser)

rating = pd.merge( data, user, on = 'user id')
print rating


data和user的类型都是dataframe,用user id做为主键连接两张表,data和user中都有数据,
输出如下:
Empty DataFrame
Columns: [user id, item id, rating, timestamp, gender]
Index: []

怎么会是空的呢?
...全文
3594 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cehui120 2019-09-04
  • 打赏
  • 举报
回复
请问问题出在哪里 既然都是string类型?
nixing123 2016-10-11
  • 打赏
  • 举报
回复
十分感谢!按照你的思路解决了问题。 不过,你给出的这一段
df = pd.DataFrame(data,columns=datafeilds)
运行到这就出错了 我在variable explorer看了data的值,显示只有1列,所以我自己用代码清理了一下。
data = np.loadtxt('u.data',delimiter = " ", dtype=str)
datafeilds = ['user id', 'item id', 'rating', 'timestamp']

sline = []

for line in data:
    sline.append( line.split('\t') )
    
array = np.array(sline)
df = pd.DataFrame(array,columns=datafeilds)
最后,还是想请教一下,为何会出现题目中的问题呢? 两个数据都是<class 'pandas.core.frame.DataFrame'> user id也都是<type 'str'> 到底哪里不一样呢? 再次感谢!
sprawling 2016-10-11
  • 打赏
  • 举报
回复
因为看不到你的表格数据,我估计可能是你user id 中的数据没有相同的,这样就会导致merge以后为空表。
nixing123 2016-10-10
  • 打赏
  • 举报
回复
引用 1 楼 uiuiy1 的回复:
因为两个表的user id没有一样的,注意它们的类型
print data.columns[0], user.columns[1]
print type(data.columns[0]), type(user.columns[1])
输出为 user id user id <type 'str'> <type 'str'> 都是string啊 在variable explorer查看rating,有如下错误 TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
sprawling 2016-10-10
  • 打赏
  • 举报
回复
#!/usr/bin/python
# -*- coding:utf-8 -*-

import pandas as pd
import numpy as np

#读取数据,更换列表名
data = np.loadtxt('u.data',delimiter = " ", dtype=str)
#data = pd.read_table( 'u.data' )
datafeilds = ['user id', 'item id', 'rating', 'timestamp']
#data.columns = datafeilds
df = pd.DataFrame(data,columns=datafeilds)

#将文件中需要的数据分别存入两个列表
user_id = []
gender = []
userfile = open('u.user')
contents = userfile.readlines()
userfile.close()
for i in range(0,len(contents)):
line = contents[i].split('|')
user_id.append( line[0] )
gender.append( line[2] )

#存入数据,为dataframe类型
dictuser = {'user id':user_id, 'gender':gender}
user = pd.DataFrame(dictuser)

rating = pd.merge( df, user, on = 'user id')
print rating

u.data
11 1 1 y1
22 2 2 y1
33 3 3 y1
44 4 4 y1
55 5 5 y1
66 6 6 y1

u.user
11|m|f
22|f|f
33|m|f
44|m|f
55|f|f
66|f|f


m不器 2016-10-10
  • 打赏
  • 举报
回复
楼主 问题解决了吗 同求答案
屎克螂 2016-09-27
  • 打赏
  • 举报
回复
因为两个表的user id没有一样的,注意它们的类型

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧