Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

enter image description hereTarget: I have a pandas data frame, as shown below, with multiple of the column and would like to get the subtotal for few columns and write "Total" at Placement# Name.

DataFrame: enter image description here

My Attempt:

**adsize_sales_second_table.loc["Grand Total"] = pd.Series(adsize_sales_second_table.loc[:, ["Delivered Impressions",

"Clicks",
"Conversion", "Spend"]].sum(),

                                                        

                                                             index=["Delivered Impressions", "Clicks", "Conversion", "Spend"]**

)

This is adding a row at last and can't be figuring out to fill subtotal: enter image description here

Expected Output: I'd have expected the output to be as followed enter image description here

See edited code:

adsize_sales_data = adsize_sales_second_table.loc[:, ["Placement# Name", "Adsize", "Delivered Impressions", "Clicks",

                                                              "CTR", "Conversion", "Conversion Rate", "Spend", "eCPA"]]

        cols = ["Delivered Impressions", "Clicks", "Conversion", "Spend"]

        adsize_sales_data['Placement# Name'] = adsize_sales_data['Placement# Name'].ffill()

        grand = adsize_sales_data[cols].sum()

        grand.loc['Placement# Name'] = 'Grand total'

        adsize_sales_data_new = adsize_sales_data.groupby('Placement# Name')[cols].sum()

        adsize_sales_data_new.index = adsize_sales_data.index.astype(str)+'____'

        adsize_sales_data = (pd.concat([adsize_sales_data.set_index('Placement# Name'), adsize_sales_data_new], keys=('a', 'b')).sort_index(level=1).reset_index())

        adsize_sales_data['Placement# Name'] = np.where(adsize_sales_data ['level_0'] == 'a', adsize_sales_data['Placement# Name'], 'Total')

        adsize_sales_data = adsize_sales_data.drop('level_0', axis=1)

        adsize_sales_data.loc[len(adsize_sales_data.index)] = grand

        print (adsize_sales_data)

t is now giving values error.enter image description here

1 Answer

0 votes
by (41.4k points)

Use this below code:

#specify columns to sum

cols = ["Delivered Impressions", "Clicks", "Conversion", "Spend"]

#replace NaNs by forward filling

df['Placement# Name'] = df['Placement# Name'].ffill()

#count grand total

grand = df[cols].sum()

grand.loc['Placement# Name'] = 'Grand total'

print (grand)

#get subtotal by aggregation sum

df1 = df.groupby('Placement# Name')[cols].sum()

#change sum for correct order

df1.index = df1.index + '____'

#create empty DataFrame

df2 = pd.DataFrame(index=df1.index + '__')

df = pd.concat([df.set_index('Placement# Name'), df1, df2], keys=('a', 'b', 'c')).sort_index(level=1).reset_index()

#get output by 2 conditions

m1 = df['level_0'] == 'a'

m2 = df['level_0'] == 'c'

df['Placement# Name'] = np.select([m1, m2], [df['Placement# Name'], np.nan], default='Total')

df = df.drop('level_0', axis=1)

df.loc[len(df.index)] = grand

print (df)

0   13.iab units (mobile only) + non-expanding adh...   320x50    0.0119683   

1                                               Total      NaN          NaN   

2                                                 NaN      NaN          NaN   

3   15.iab units (mobile only) + non-expanding adh...   320x50    0.0138741   

4   15.iab units (mobile only) + non-expanding adh...   768x90    0.0271041   

5                                               Total      NaN          NaN   

6                                                 NaN      NaN          NaN   

7                     18.iab units - desktop + mobile  160x600   0.00155927   

8                     18.iab units - desktop + mobile  300x250   0.00797965   

9                     18.iab units - desktop + mobile  300x600   0.00275059   

10                    18.iab units - desktop + mobile   728x90   0.00496391   

11                                              Total      NaN          NaN   

12                                                NaN      NaN          NaN   

13  4.iab units (mobile only) + non-expanding adhe...   320x50    0.0141497   

14                                              Total      NaN          NaN   

15                                                NaN      NaN          NaN   

16  5.iab units (mobile only) + non-expanding adhe...   320x50    0.0111654   

17  5.iab units (mobile only) + non-expanding adhe...   768x90    0.0253428   

18                                              Total      NaN          NaN   

19                                                NaN      NaN          NaN   

20                     6.iab units - desktop + mobile  160x600  7.41895e-05   

21                     6.iab units - desktop + mobile  300x250     0.011838   

22                     6.iab units - desktop + mobile  300x600  0.000259538   

23                     6.iab units - desktop + mobile   728x90   0.00538178   

24                                              Total      NaN          NaN   

25                                                NaN      NaN          NaN   

26                                        Grand total      NaN          NaN   

   Clicks Conversion Conversion Rate Delivered Impressions    Spend     eCPA  

0    1888          4     2.53566e-05                157750  1126.79  281.696  

1    1888          4             NaN                157750  1126.79      NaN  

2     NaN        NaN             NaN                   NaN      NaN      NaN  

3    2121         17     0.000111202                152875  1091.96  64.2332  

4     152          2     0.000356633                  5608  40.0571  20.0286  

5    2273         19             NaN                158483  1132.02      NaN  

6     NaN        NaN             NaN                   NaN      NaN      NaN  

7      37         21     0.000884993                 23729  132.204  6.29545  

8     684         58     0.000676637                 85718  477.572    8.234  

9      34         13      0.00105169                 12361  68.8684  5.29757  

10    403         80     0.000985392                 81186  452.322  5.65403  

11   1158        172             NaN                202994  1130.97      NaN  

12    NaN        NaN             NaN                   NaN      NaN      NaN  

13   3840         23     8.47511e-05                271383  1938.45  84.2804  

14   3840         23             NaN                271383  1938.45      NaN  

15    NaN        NaN             NaN                   NaN      NaN      NaN  

16   1127          4     3.96287e-05                100937  720.979  180.245  

17    183          0               0                  7221  51.5786        0  

18   1310          4             NaN                108158  772.557      NaN  

19    NaN        NaN             NaN                   NaN      NaN      NaN  

20      1          0               0                 13479  75.0973        0  

21    792          0               0                 66903  372.745        0  

22      1          0               0                  3853  21.4667        0  

23    266          0               0                 49426  275.373        0  

24   1060          0             NaN                133661  744.683      NaN  

25    NaN        NaN             NaN                   NaN      NaN      NaN  

26  11529        222             NaN           1.03243e+06  6845.46      NaN  

If you wish to learn Pandas visit this Pandas Tutorial.

Related questions

Browse Categories

...