根据熊猫中的groupby地图添加新列

时间:2020-06-21 17:27:12

标签: pandas pandas-groupby

我有一个如下所示的df

subProjectB

从上面我想添加一列称为cost_per_unit,如下所示。

产品A的成本为100,B为500,C为200

product     bought_date      number_of_sales 
A           2016             15
A           2017             10
A           2018             15
B           2016             20
B           2017             30
B           2018             20
C           2016             20
C           2017             30
C           2018             20

预期输出:

d1 = {'A':100, 'B':500, 'C':'200'}

2 个答案:

答案 0 :(得分:1)

不需要任何 lambda 函数。只需运行:

df['cost_per_unit'] = df['product'].map(d1)

附加说明: product Pandas 函数的名称。你应该避免 列名称“覆盖”现有功能或属性。 这是一个好习惯,至少在char情况下,它们应该有所不同。

答案 1 :(得分:0)

您可以尝试以下方法:

df['cost_per_unit'] = df.apply(lambda x: d1[x['product']], axis=1)
print(df)

  product  bought_date  number_of_sales cost_per_unit
0       A         2016               15           100
1       A         2017               10           100
2       A         2018               15           100
3       B         2016               20           500
4       B         2017               30           500
5       B         2018               20           500
6       C         2016               20           200
7       C         2017               30           200
8       C         2018               20           200