且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python Pandas-Concat两个具有不同行数和列数的数据框

更新时间:2022-11-17 16:16:47

我相信 DataFrame.merge 在这种情况下会起作用:

I believe that DataFrame.merge would work in this case:

# use how='outer' to preserve all information from both DataFrames
df1.merge(df2, how='outer', on='customer_id')

DataFrame.join 也可以工作这两个DataFrame的索引都设置为 customer_id (也更简单):

DataFrame.join could also work if both DataFrames had their indexes set to customer_id (it is also simpler):

df1 = df1.set_index('customer_id')
df2 = df2.set_index('customer_id')
df1.join(df2, how='outer')