How to use df.groupby() to select and sum specific columns w/o pandas trimming total number of columns
I got Column1, Column2, Column3, Column4, Column5, Column6
I'd like to group Column1 and get the row sum of Column3,4 and 5
When I apply groupby() and get this that is correct but it's leaving out Column6:
df = df.groupby(['Column1'])[['Column3', 'Column4', 'Column5']].sum
I tried with this but it doesn't group according to Column1 and it doesn't sum anything, but I get all my columns:
df.sort_values(['Column1']).groupby(['Column3', 'Column4', 'Column5']).sum()
How can I use groupby() correctly in this case?
Thank you!
I add my code:
df = df.drop(['Position', 'Swap', 'S / L', 'T / P'], axis=1)
df = df.groupby(['Symbol']).agg({'Profit': ['sum'], 'Volume': ['sum'], 'Commission': ['sum'], 'Time': pd.Series.mode})
df['Comm. ratio'] = (df['Commission'] / df['Profit'])
df['Comm. ratio'] = df['Comm. ratio'].round(2)
df['Net profit'] = (df['Profit'] + df['Commission'])
df.loc['Total'] = pd.Series([df['Commission'].sum(),df['Profit'].sum(),df['Net profit'].sum()], index = ['Commission','Profit','Net profit'])
The output is:
As you can see it adds sum and mode rows that I'd like not to have.
Moreover, it ignores the df.loc['Total'] code and it leaves the Total row empty.
Topic groupby automatic-summarization pandas python
Category Data Science