简体   繁体   English

根据条件将两个 pandas 数据帧合并为一个

[英]Combining two pandas dataframes into one based on conditions

I got two dataframes, simplified they look like this:我有两个数据框,简化后看起来像这样:

Dataframe A Dataframe A

ID ID item物品
1 1 apple苹果
2 2 peach

Dataframe B Dataframe B

ID ID flag旗帜 price ($)价格(美元)
1 1 A一个 3 3
1 1 B 2 2
2 2 B 4 4
2 2 A一个 2 2

ID: unique identifier for each item flag: unique identifier for each vendor price: varies for each vendor ID:每个项目的唯一标识符标志:每个供应商的唯一标识符价格:每个供应商的不同

In this simplified case I want to extract the price values of dataframe B and add them to dataframe A in separate columns depending on their flag value.在这个简化的例子中,我想提取 dataframe B 的价格值并将它们添加到 dataframe A 的单独列中,具体取决于它们的标志值。

The result should look similar to this Dataframe C结果应该类似于此Dataframe C

ID ID item物品 price_A价格_A price_B价格_B
1 1 apple苹果 3 3 2 2
2 2 peach 2 2 4 4

I tried to split dataframe B into two dataframes the different flag values and merge them afterwards with dataframe A, but there must be an easier solution.我试图将 dataframe B 拆分为具有不同标志值的两个数据帧,然后将它们与 dataframe A 合并,但必须有一个更简单的解决方案。

Thank you in advance: :)先感谢您: :)

*edit: removed the pictures *编辑:删除图片

You can use pd.merge and pd.pivot_table for this:您可以为此使用pd.mergepd.pivot_table

df_C = pd.merge(df_A, df_B, on=['ID']).pivot_table(index=['ID', 'item'], columns='flag', values='price')
df_C.columns = ['price_' + alpha for alpha in df_C.columns]

df_C = df_C.reset_index()

Output: Output:

>>> df_C
   ID   item  price_A  price_B
0   1  apple        3        2
1   2  peach        2        4
(dfb
 .merge(dfa, on="ID")
 .pivot_table(index=['ID', 'item'], columns='flag', values='price ($)')
 .add_prefix("price_")
 .reset_index()
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM