Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用键合并两个熊猫系列_Python_Pandas_Merge - Fatal编程技术网

Python 使用键合并两个熊猫系列

Python 使用键合并两个熊猫系列,python,pandas,merge,Python,Pandas,Merge,我有两个熊猫系列,即x和y x、 head()给出: y、 head()给出: 我需要的是合并这两个列,其中user=UserID 例如,我的第一行应该如下所示: user hotel rating id UserID Gender Age Occupation Zip Code 0 1 1253 5 2783_1253 1.0 F 18.0 10.0 48067 如何获得它?我想您

我有两个熊猫系列,即x和y

x、 head()给出:

y、 head()给出:

我需要的是合并这两个列,其中user=UserID

例如,我的第一行应该如下所示:

   user  hotel  rating      id         UserID Gender   Age  Occupation Zip Code
0  1    1253      5      2783_1253     1.0      F     18.0     10.0     48067

如何获得它?

我想您需要首先将
float
列转换为
int
,然后:

或者将两列都转换为浮动:

x['UserID'] = x.user.astype(float)
df = pd.merge(x,y, on='UserID')
print (df)
   user  hotel  rating         id  UserID  Gender Age  Occupation   Zip   Code
0     1   1253       5  2783_1253     1.0     2.0   M        56.0  16.0  70072
1     4    589       5   2783_589     4.0     5.0   M        25.0  20.0  55455
2     3   1274       4  2783_1274     3.0     4.0   M        45.0   7.0   2460
3     2    741       5   2783_741     2.0     3.0   M        25.0  15.0  55117

您正在寻找的是加入。您可以在这里找到答案:(它的工作原理与SQL中的工作原理相同)。
但是,如果您希望将
user
保持为整数,将
UserID
保持为浮点数,则可能需要进行一些额外的强制转换和重命名。

Hi。谢谢,它帮了我。
   user  hotel  rating      id         UserID Gender   Age  Occupation Zip Code
0  1    1253      5      2783_1253     1.0      F     18.0     10.0     48067
y['user'] = y.UserID.astype(int)
df = pd.merge(x,y, on='user')
print (df)
   user  hotel  rating         id  UserID  Gender Age  Occupation   Zip   Code
0     1   1253       5  2783_1253     1.0     2.0   M        56.0  16.0  70072
1     4    589       5   2783_589     4.0     5.0   M        25.0  20.0  55455
2     3   1274       4  2783_1274     3.0     4.0   M        45.0   7.0   2460
3     2    741       5   2783_741     2.0     3.0   M        25.0  15.0  55117
x['UserID'] = x.user.astype(float)
df = pd.merge(x,y, on='UserID')
print (df)
   user  hotel  rating         id  UserID  Gender Age  Occupation   Zip   Code
0     1   1253       5  2783_1253     1.0     2.0   M        56.0  16.0  70072
1     4    589       5   2783_589     4.0     5.0   M        25.0  20.0  55455
2     3   1274       4  2783_1274     3.0     4.0   M        45.0   7.0   2460
3     2    741       5   2783_741     2.0     3.0   M        25.0  15.0  55117