我有两个数据框:
df1 = [[' ','User','Area'], # Columns are 'User' and 'Area'
['Row1','Brin25','Senior'],
['Row2','Ton55','Junior'],
['Row3','Brown22','Experienced'],
['Row4','Lorst45','Beginner']]
___________________________________________________________________________
df2 = [[' ', 'Date', 'User','Class'],
['Row1','06/14/2019','Brin25','Diamond'],
['Row2','06/14/2019','Brin25','Diamond'],
['Row3','06/14/2019','Brin25','Diamond'],
['Row4','06/14/2019','Lorst45','Gold']]
.
.
.
.
我想要得到什么:
merged_df = [[' ', 'Date', 'User', 'Class', 'Area'], #merged Area column
['Row1','06/14/2019','Brin25','Diamond', 'Senior'],
['Row2','06/14/2019','Brin25','Diamond', 'Senior'],
['Row3','06/14/2019','Brin25','Diamond', 'Senior'],
['Row4','06/14/2019','Lorst45','Gold', 'Beginner']]
.
.
.
.
因此,通过同时使用User
和df1
中的列df2
,我想将Area
中的df1
的值附加到新合并的列中< / p>
我尝试过
combined = df1[df1['User'].isin(df2['User'].tolist())]
merged_df = pd.merge(combined, df2)
所有列均显示,但所有值均消失(零行)