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)
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With