I have a DataFrame of records that looks something like this:
stocks = pd.Series([\'A\', \'A\', \'B\', \'C\', \'C\'], name = \'stock\')
positions = pd.Series([ 1
Step 1. Use [['positions']] instead of ['positions']:
In [30]: df2 = df.groupby(['stock','same1','same2'])[['positions']].sum()
In [31]: df2
Out[31]:
positions
stock same1 same2
A AA AAA 300
B BB BBB 300
C CC CCC 900
Step 2. And then use reset_index
to move the index back to the column
In [34]: df2.reset_index()
Out[34]:
stock same1 same2 positions
0 A AA AAA 300
1 B BB BBB 300
2 C CC CCC 900
Seems my method is not so good.
Thanks to @Andy and @unutbu , you can achieve your goal by more elegant ways:
method 1:
df.groupby(['stock', 'same1', 'same2'])['positions'].sum().reset_index()
method 2:
df.groupby(['stock', 'same1', 'same2'], as_index=False)['positions'].sum()