简体   繁体   English

创建新列,其中来自两个不同数据帧的两列相同

[英]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.

相关问题 基于来自不同数据帧的两列(不同长度)创建新列 - Create new column based on two columns (different length) from different data frames Pandas - 匹配两个数据帧中的两列,并在df1中创建新列 - Pandas - match two columns from two data frames and create new column in df1 如何基于两个不同数据帧的列创建新的df? - How to create new df based on columns of two different data frames? 从两个现有数据框创建两个新数据框,同时考虑其多索引列及其值的所有组合 - Create two new data frames from two existing data frames, taking into account all combinations of their multi-index columns and their values 如何比较具有相同列但行数不同的两个数据帧? - How to compare two data frames with same columns but different number of rows? 并排比较相同列但不同行的两个数据框 - Compare two data frames side by side for same columns but different rows 熊猫:通过比较2个不同数据框中的2个列来创建新列 - Pandas: Create a new column by comparing 2 columns in 2 different data frames 在两个 pandas 数据帧中查找值并创建新列 - Looking up values in two pandas data frames and create new columns 根据两个数据框列的匹配创建另一列 - create another column based on matching of two data frames columns 如果两个不同数据帧中两列的值匹配,则将df2中另一列的值复制到df1中的列 - If values from two columns in two different data frames match then copy values from another column in df2 to column in df1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM