[英]Create new column where two columns from two different data frames are the same
I have two dataframes. 我有两个数据框。
1: 1:
pid cluster
7993355 0 0
8180238 0 5
8174589 0 7
8168267 0 10
8264548 0 10
8252159 0 0
8388741 0 6
8346358 0 2
8194226 0 8
8187866 0 3
8133728 0 1
8215624 0 6
8124250 0 0
8382996 0 5
8151852 0 0
8130044 0 2
8017035 0 5
8108438 0 0
8245152 0 1
8047538 0 3
8070691 0 7
8344660 0 5
8148647 0 6
8157608 0 10
8352127 0 8
2: 2:
pid cluster count
0 0 0 8
1 0 1 2
2 0 2 3
3 0 3 2
4 0 4 1
5 0 5 5
6 0 6 4
7 0 7 3
8 0 8 4
9 0 10 3
My goal is to join these two dataframes when both pid and cluster are the same for example if the pid and cluster are both 0 I would like the next dataframe to have the value 8 for count. 我的目标是在pid和cluster都相同的情况下将这两个数据帧连接在一起,例如,如果pid和cluster都为0,我希望下一个数据帧的计数值为8。 I would like to this autonomously.
我想自主地这样做。 I have tried using a fucntion :
train['count'] = np.where(((sample['pid'] == train['pid'])&(sample['cluster']==train['cluster'])), sample['count'], 0)
But it doesnt work. 我试过使用一个功能:
train['count'] = np.where(((sample['pid'] == train['pid'])&(sample['cluster']==train['cluster'])), sample['count'], 0)
但这不起作用。 Pd.Merge etc will not work as both are of different dimensions I have only provide a small snippet of the dataframes. Pd.Merge等将无法正常工作,因为两者的尺寸都不同。我只提供了一小段数据框。 Any help would be appreciated!!
任何帮助,将不胜感激!!
尝试这个
df2[df2['pid'].isin(pd.unique(df1['pid'])) & df2['count'].isin(pd.unique(df1['count'])) ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.