简体   繁体   English

使用 Pandas join 填充列

[英]Using Pandas join to fill in columns

I have two DataFrames that roughly look like我有两个大致看起来像的数据帧

(ID) (Category) (Value1)  (Value2)

111   1          5          7
112   1          3          8
113   2          6          9
114   3          2          6

and

(Category)  (Value1 Average for Category) (Value2 Average for Category)

1              4                              5 
2              6                              7
3              9                              2

Ultimately, I'd like to join the two DataFrames so that each ID can have the average value for its category in the row with it.最终,我想加入两个数据帧,以便每个 ID 可以在它所在的行中具有其类别的平均值。 I'm having trouble finding the right way to join/merge/etc.我无法找到加入/合并/等的正确方法。 that will fill in columns by checking the category from the other DateFrame.这将通过检查来自其他 DateFrame 的类别来填充列。 Does anyone have any idea where to start?有谁知道从哪里开始?

只需在第一个数据帧上执行df1.groupby(['ID', 'Category']).transform(func='mean')即可获得所需的数据帧。

You are simply looking for a join , in pandas we use pd.merge for that like the following:您只是在寻找join ,在pd.merge中我们使用pd.merge ,如下所示:

df3 = pd.merge(df1, df2, on='Category')

    ID  Category    Value1  Value2  Value 1 Average Value 2 Average
0   111 1           5       7       4               5
1   112 1           3       8       4               5
2   113 2           6       9       6               7
3   114 3           2       6       9               2

Official documentation of pandas on merging: pandas合并的官方文档:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html

Here is a good explanation on joins: Pandas Merging 101这是关于连接的一个很好的解释: Pandas Merging 101

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

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