简体   繁体   中英

Create New Pandas Column from Groupby and Dividing Other Columns

I have a Dataframe df like:

    Name     Date Item  Quantity  Unit_Cost  Value
0   Clay  2018_Q1   AA         9       8.97  80.73
1   Clay  2018_Q1   BB         3      12.34  37.02
2   Clay  2018_Q1   CC         4       1.40   5.60
3   Clay  2018_Q1   DD         7       0.22   1.54
4    Ray  2018_Q1   AA         8       5.30  42.40
5    Ray  2018_Q1   DD         2       1.60   3.20
6    Ray  2018_Q1   EE         4       8.00  32.00
7    Ray  2018_Q1   FF         9       4.00  36.00
8   Clay  2018_Q2   DD         1       0.45   0.45
9   Clay  2018_Q2   AA         4       7.00  28.00
10  Clay  2018_Q2   EE         2       6.40  12.80
11  Clay  2018_Q2   CC         3       2.30   6.90
12   Ray  2018_Q2   CC         1       9.00   9.00
13   Ray  2018_Q2   DD         4       8.00  32.00
14   Ray  2018_Q2   GG         1       6.50   6.50
15   Ray  2018_Q2   HH         2       9.10  18.20

nd I want to calculate a new column called % of Total Value , which will be the:

( Value for a given Date Name , and Item ) / ( sum of all Values for that Date and Name ).

So output would look like:

    Name     Date Item  Quantity  Unit_Cost  Value  % of Total Value
0   Clay  2018_Q1   AA         9       8.97  80.73          0.646409
1   Clay  2018_Q1   BB         3      12.34  37.02          0.296421
2   Clay  2018_Q1   CC         4       1.40   5.60          0.044839
3   Clay  2018_Q1   DD         7       0.22   1.54          0.012331
4    Ray  2018_Q1   AA         8       5.30  42.40          0.373239
5    Ray  2018_Q1   DD         2       1.60   3.20          0.028169
6    Ray  2018_Q1   EE         4       8.00  32.00          0.281690
7    Ray  2018_Q1   FF         9       4.00  36.00          0.316901
8   Clay  2018_Q2   DD         1       0.45   0.45          0.009346
9   Clay  2018_Q2   AA         4       7.00  28.00          0.581516
10  Clay  2018_Q2   EE         2       6.40  12.80          0.265836
11  Clay  2018_Q2   CC         3       2.30   6.90          0.143302
12   Ray  2018_Q2   CC         1       9.00   9.00          0.136986
13   Ray  2018_Q2   DD         4       8.00  32.00          0.487062
14   Ray  2018_Q2   GG         1       6.50   6.50          0.098935
15   Ray  2018_Q2   HH         2       9.10  18.20          0.277017

I have tried something like:

quarter_total_value = df.groupby(['Name', 'Date'])['Value'].sum()

df['% of Total Value'] = df['Value']/quarter_total_value

but that gives me an error:

ValueError: cannot join with no overlapping index names

So how can I achieve the desired output? Do I have to iterate over the df and write each % of Total Value or is there an easier way of doing this?

You can check with transform

quarter_total_value = df.groupby(['Name', 'Date'])['Value'].transform('sum')

df['% of Total Value'] = df['Value']/quarter_total_value

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.

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