I have a table with the following columns:
date Product category units xx_col ..
0 2017-02-11 Books heavy 2. 11.
1 2017-02-11 Books medium 1. 22.
2 2017-02-11 Books light 1. 11.
3 2017-02-11 DVD heavy 3. 11.
4 2017-02-11 DVD medium 2. 4170.775
5 2017-02-11 DVD light 2. 4170.775
Now, I want to create another two columns with a sum of units for a given product (all three categories) for every date, and then the fraction. as shown below. There are also other columns xx_col, which I want to preserve in the table ( I do not mention it in the second table, but it should be there).
date Product category units unit_tot unit_frac ..
0 2017-02-11 Books heavy 2. 4. .5
1 2017-02-11 Books medium 1. 4 .25
2 2017-02-11 Books light 1. 4 .25
3 2017-02-11 DVD heavy 3. 10 .3
4 2017-02-11 DVD medium 2. 10 .2
5 2017-02-11 DVD light 5. 10 .5
unit_tot: total_units- sum for a given product for a given date over the three (h, m, l) categories (2+1+1=4).
unit_frac: unit/ unit_tot I want both of these not just the unit_tot or unit_frac, and I want to keep this table as is. I could get two different pivot_tables, but I want to see if there is an easy way to add these two columns.
IIUC, just need to transform
df['unit_tot'] = df.groupby(['date', 'Product']).units.transform('sum')
Then divide both columns to get the fraction
df['unit_frac'] = df['units']/df['unit_tot']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.