I am doing some task in pandas python.
I have a data like this:
col1 |col2 |col3 |col4 |col5 | col6
delhi |assam |"f" |78.3 |87.1 | B2C
delhi |goa |"f" |78.3 |87.1 | B2C
delhi |goa |"f" |78.3 |87.1 | B2C
delhi |assam |"f" |78.3 |87.1 | B2C
up |assam |"f" |78.3 |87.1 | B2B
delhi |assam |"f" |78.3 |87.1 | B2B
Now I want to filter those rows where col6 is B2C. After filtering , I want to groupby col1 and col2 and sum the col4 and col5.
So Output should be like:
col1 |col2 |col3 |col4 |col5 | col6
delhi |assam |"f" |156.6|174.2| B2C
delhi |goa |"f" |156.6|174.2| B2C
up |assam |"f" |78.3 |87.1 | B2B
delhi |assam |"f" |78.3 |87.1 | B2B
The approach I have tried:
df.loc[df['col6'] == 'B2C'].groupby(['col1', 'col2']).agg({'col4':'sum', 'col5':'sum'})
But I don't know how to append this result with original data frame. Also guide me if I can do something better than this.