如何组合三个数据框,如下所示?
前两者的主要关系必须基于ID1,因为它是两个数据帧之间的匹配关系。
第三个数据帧,Address2 必须匹配才能添加到哈希中
Df1:
Name1 Name2 Name3 Address ID1 ID2 Own
Matt John1 Jill 878 home 1 0 Deal
Matt John2 Jack 879 home 2 1 Dael
DF2:
Name1 ID1 Address Name4 Address2
Matt 1 878 home face1 face\123
Matt 1 878 home face2 face\345
Matt 1 878 home face3 face\678
Matt 2 879 home head1 head\123
Matt 2 879 home head2 head\345
Matt 2 879 home head3 head\678
DF3:
Address2 Hash
face\123 abc123
face\345 cde321
face\678 efg123
head\123 123efg
head\345 efg321
head\678 acd321
我正在尝试将三个数据框组合成一个,如下所示:
Name1 Name2 ID1 Address Own Name3 ID2 Name4 Address2 Hash
Matt John1 1 878 home Deal Jill 0 face1 face\123 abc123
Matt John1 1 878 home Deal Jill 0 face2 face\345 cde321
Matt John1 1 878 home Deal Jill 0 face3 face\678 efg123
Matt John2 2 879 home Dael Jack 1 head1 head\123 123efg
Matt John2 2 879 home Dael Jack 1 head2 head\345 efg321
Matt John2 2 879 home Dael Jack 1 head3 head\678 acd321
在 df1 和 df2 之间,关键是 Id1 df2 和 df3 之间的关键是 Address2
非常感谢您的帮助。
最佳答案
看看 merge功能,可以找到一些例子here .对于您的具体问题,请尝试以下操作:
combined_df = df1.merge(df2, on="Id1", how="inner").merge(df3, on="Adress2", how="inner")
关于python - 根据条件合并 3 个不同的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42701576/