简体   繁体   中英

How to add new columns to pivot table using pandas

I'm trying to create a new column that show the weightage of every product that I has.

Let's say I have the following dataframe that I have pivot:

   PRODUCT  UNIT_TESTED AVG_YIELD 
        A       401    82.1042
        B      1512    96.0687  
        C       292    22.7806  
        D       134    37.0088

using

 pd.pivot_table(data = df, index = ['PRODUCT'], 
                  values = ("UNIT_TESTED","AVG_YIELD"), 
                  aggfunc = "sum", margins=True)\
     .fillna('')

Now, I want to add a new column WEIGHTAGE for each product.

The calculation:

WEIGHTAGE 'A' = (UNIT_TESTED 'A'/Total of UNIT_TESTED)*100

This is where I'm stuck to put into coding to create a new column.

My desired output:

PRODUCT UNIT_TESTED AVG_YIELD WEIGHTAGE
    A       401      82.1042    17.1441
    B      1512      96.0687    64.6430
    C       292      22.7806    12.4840
    D       134      37.0088    5.7289

The last row of the pivot table that you obtained contains the sum of unit tested. So you can simply divide by that value ( pivot_df.loc["All","UNIT_TESTED"] ) the column UNIT_TESTED :

pivot_df = pd.pivot_table(data = df, index = ['PRODUCT'], 
                  values = ("UNIT_TESTED","AVG_YIELD"), 
                  aggfunc = "sum", margins=True)\
     .fillna('')

pivot_df["Weightage"] = round((pivot_df["UNIT_TESTED"] / pivot_df.loc["All","UNIT_TESTED"])*100,2)

print(pivot_df)

Output:

    AVG_YIELD   UNIT_TESTED Weightage
PRODUCT         
A   82.1042     401        17.14
B   96.0687     1512       64.64
C   22.7806     292        12.48
D   37.0080     134        5.73
All 237.9615    2339       100.00

suppose, your pivot table is pivot_df

pivot_df['WEIGHTAGE'] = (pivot_df['UNIT_TESTED'] * 100 ) / pivot_df['UNIT_TESTED'].sum()

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