Python 更新数据帧的值

Python 更新数据帧的值,python,pandas,dataframe,Python,Pandas,Dataframe,给出一些关于菜肴、餐厅位置和销售额的数据: >>> import pandas >>> df1 = pandas.DataFrame({"dish" : ["fish", "chicken", "fish", "chicken", "chicken"], ... "location" : ["central", "central", "north", "north", "south"], ...

给出一些关于菜肴、餐厅位置和销售额的数据:

>>> import pandas
>>> df1 = pandas.DataFrame({"dish"     : ["fish", "chicken", "fish", "chicken", "chicken"],
...                         "location" : ["central", "central", "north", "north", "south"],
...                         "sales" : [1,3,5,2,4]})
>>> df1
      dish location  sales
0     fish  central      1
1  chicken  central      3
2     fish    north      5
3  chicken    north      2
4  chicken    south      4

>>> df2 = df1[["dish", "location"]]
>>> df2["sales_contrib"] = 0.0
>>> df2
      dish location  sales_contrib
0     fish  central            0.0
1  chicken  central            0.0
2     fish    north            0.0
3  chicken    north            0.0
4  chicken    south            0.0
现在,我想做以下工作:

  • 遍历df2的每一行
  • 计算销售费用。那道菜的位置。因此,对于鱼类,中部地区占总收入的16.67%,而北部地区占总收入的83.3%
  • 结果df为

              dish location  sales_contrib
    0     fish  central            16.67
    1  chicken  central            33.33
    2     fish    north            83.33
    3  chicken    north            22.22
    4  chicken    south            44.45
    

    我尝试使用
    iteritems()
    ,但没有得到结果

    你可以利用熊猫的力量来做到这一点

    dish_totals = df1.groupby(by="dish").sum()
    df2["sales_contrib"] = df1.apply((lambda row: 100*row["sales"]/dish_totals.loc[row["dish"]]), axis=1)
    print(df2)
    
    输出:

          dish location  sales_contrib
    0     fish  central      16.666667
    1  chicken  central      33.333333
    2     fish    north      83.333333
    3  chicken    north      22.222222
    4  chicken    south      44.444444
    
    试一试


    @瓦伊沙利,谢谢。。。我也喜欢你的答案,因为你没有中间人
    dish\u totals
    dataframe。另外,我不确定这是否重要,但我可以像你一样四舍五入到小数点后两位。
    (df1.groupby(['dish', 'location']).sales.sum().div(df1.groupby('dish').sales.sum()) * 100).round(2).reset_index()
    
        dish    location    sales
    0   chicken central     33.33
    1   chicken north       22.22
    2   chicken south       44.44
    3   fish    central     16.67
    4   fish    north       83.33