Given some data about dishes, the location of the restaurant and its sales:
>>> 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
Right now, I would like to do the following:
- Iterate through each row of
df2 - Calculate the sales contrib. of that location for that dish . So for fish, central contributes 1/6 16.67% of total revenue and north contributes the remaining 83.3%
The resultant df is
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
I tried using iteritems() but could not get results.