基于一列的值合并两个数据框

时间:2019-06-14 16:14:57

标签: python pandas numpy dataframe merge

我有两个数据框:


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']]
                        .
                        .
                        .
                        .

因此,通过同时使用Userdf1中的列df2,我想将Area中的df1的值附加到新合并的列中< / p>

我尝试过

combined = df1[df1['User'].isin(df2['User'].tolist())]
merged_df = pd.merge(combined, df2)

所有列均显示,但所有值均消失(零行)

0 个答案:

没有答案