简体   繁体   中英

Using Groupby/ Pivot Tables with Pandas MultiIndex DataFrame (Python)

I have the following MultiIndex Pandas Dataframe.

                        Asset Price  Quantity Traded
Date       Asset                        
2015-01-01 Ripple     0.024455         7
           Bitcoin  320.440000         3
2015-01-02 Ripple     0.024377         1
           Bitcoin  314.080000         -10
2015-01-03 Ripple     0.024297         4
           Bitcoin  314.850000         5
2015-01-04 Ripple     0.022100         12
           Bitcoin  281.150000         -3
2015-01-05 Ripple     0.018943         15
           Bitcoin  265.080000         5

What's a quick way to get the net amount traded each day (as a Pandas Series)? Eg, for 2015-01-02 the net amount traded is 0.024377 * 1 + 314.08 * (-10) .

I feel I can use pivot_table or groupby but cannot figure out how.

I would really appreciate any help,

Jack

Create a new column for the total per asset per day then groupby on the date, which here is level=0 .

df = df.assign(total_asset_daily=lambda x: (x.loc[:, 'Asset Price'] *
                                            x.loc[:, 'Quantity Traded']))

df.total_asset_daily.groupby(level=0).sum()

Date          total_asset_daily
2015-01-01    961.491185
2015-01-02   -3140.775623

Alternatively, for Pandas 0.21 onward, you can use Index level names in a groupby . In this case you can use groupby('Date') as shown in Wen's answer .

df.groupby(level='Date').apply(lambda x : sum(x['Asset Price']*x['Quantity Traded']))
Out[31]: 
Date
2015-01-01     961.491185
2015-01-02   -3140.775623

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM