Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum column values based on part of index names in dataframe

Tags:

python

pandas

I have the following dataframe which is the result of a groupby operation.

Gender          F     M
Grade letter
D            NaN   1.0
D+            7.0  2.0
C-            3.0  2.0
C             3.0  4.0
C+            9.0  12.0
B-            8.0  10.0
B             6.0  3.0
B+            5.0  7.0
A-            7.0  4.0
A             2.0  4.0
A+            1.0 NaN

I want to combine grade letters like D, C, B, A ignoring the suffix (-, +).

The desired output is something like

Gender          F     M
Grade letter
D             7.0   3.0
C             15.0 18.0
B             19.0 20.0
A             10.0 8.0

I tried the solution provided here, but it did not for me.

df.groupby(df.index.to_series().str[0]).size().unstack(fill_value=0)
like image 214
Khalil Al Hooti Avatar asked Jan 16 '20 06:01

Khalil Al Hooti


People also ask

How do I sum specific columns in a data frame?

To sum given or list of columns then create a list with all columns you wanted and slice the DataFrame with the selected list of columns and use the sum() function. Use df['Sum']=df[col_list]. sum(axis=1) to get the total sum.

How do you sum the values in a column of a DataFrame pandas?

sum() function is used to return the sum of the values for the requested axis by the user. If the input value is an index axis, then it will add all the values in a column and works same for all the columns. It returns a series that contains the sum of all the values in each column.

How do I get the sum of multiple columns in pandas?

Sum all columns in a Pandas DataFrame into new column If we want to summarize all the columns, then we can simply use the DataFrame sum() method.


1 Answers

You can aggregate sum, also for first letter is possible omit .to_series():

df1 = df.groupby(df.index.str[0], sort=False).sum()
print (df1)
           F     M
Gender            
D        7.0   3.0
C       15.0  18.0
B       19.0  20.0
A       10.0   8.0
like image 150
jezrael Avatar answered Oct 12 '22 17:10

jezrael