Pandas duplicate indices amount with sum

I have a data frame indexed by date

transactions_ind
Out[25]: 
                   Ticker     Transaction  Number_of_units      Price
Date                                                                 
2012-10-11  ROG VX Equity             Buy            12000  182.00000
2012-10-16  ROG VX Equity            Sell            -5000  184.70000
2012-11-16  ROG VX Equity            Sell            -5000  175.51580
2012-12-07  ROG VX Equity             Buy             5000  184.90000
2012-12-11  ROG VX Equity            Sell            -3000  188.50000
2012-12-11  ROG VX Equity  Reversal: Sell             3000  188.50000
2012-12-11  ROG VX Equity            Sell            -3000  188.50000
2012-12-11  ROG VX Equity  Reversal: Sell             3000  188.50000
2012-12-11  ROG VX Equity            Sell            -3000  188.50000
2012-12-20  ROG VX Equity            Sell            -5000  185.80000

I want to summarize duplicate index values ​​(2012-12-11), but only above the "Number_of_units" column.

transactions_ind
Out[25]: 
                   Ticker     Transaction  Number_of_units      Price
Date                                                                 
2012-10-11  ROG VX Equity             Buy            12000  182.00000
2012-10-16  ROG VX Equity            Sell            -5000  184.70000
2012-11-16  ROG VX Equity            Sell            -5000  175.51580
2012-12-07  ROG VX Equity             Buy             5000  184.90000
2012-12-11  ROG VX Equity            Sell            -3000  188.50000
2012-12-20  ROG VX Equity            Sell            -5000  185.80000

Using

transactions_ind.groupby(transactions_ind.index).sum()

Deletes the Ticker and Transaction columns as they are filled with non-numeric values. I would also know how to handle different rows in the Transaction column when I summarize the Number_of_units columns. We hope that there is one layer in pandas. Thank you for your help!

+4
source share
1 answer

You can use aggwith firstand sum:

df = df.groupby(df.index).agg({'Ticker': 'first',
                                'Transaction': 'first',
                                'Number_of_units':sum, 
                                'Price': 'first'})
#reorder columns
df = df[['Ticker','Transaction','Number_of_units','Price']]
print df
                   Ticker Transaction  Number_of_units     Price
Date                                                            
2012-10-11  ROG VX Equity         Buy            12000  182.0000
2012-10-16  ROG VX Equity        Sell            -5000  184.7000
2012-11-16  ROG VX Equity        Sell            -5000  175.5158
2012-12-07  ROG VX Equity         Buy             5000  184.9000
2012-12-11  ROG VX Equity        Sell            -3000  188.5000
2012-12-20  ROG VX Equity        Sell            -5000  185.8000
+6
source

Source: https://habr.com/ru/post/1628789/


All Articles