简体   繁体   English

Python Pandas 汇总/列总和

[英]Python Pandas rollup/sum of columns

I am using Pandas in Python3 and looking how to add a total column to the end of the table for each asset type ( ETH and BTC ).我在 Python3 中使用 Pandas 并查看如何在表的末尾为每种资产类型( ETHBTC )添加一个总列。

positions = _pd.read_sql_query('''
        SELECT id,
               JSON_EXTRACT(data, '$.purchase_date') as purchase_date,
               JSON_EXTRACT(data, '$.asset') as asset,
               JSON_EXTRACT(data, '$.size') as size,
               JSON_EXTRACT(data, '$.filled_price') as filled_price,
               JSON_EXTRACT(data, '$.exchange') as exchange,
               ROUND(JSON_EXTRACT(data, '$.size') * JSON_EXTRACT(data, '$.filled_price'), 2) as cost
          FROM positions
      ORDER BY purchase_date ASC
    ''', _conn)

Which output is like:哪个输出是这样的:

+----+---------------------+-------+------------+--------------+-------------------+---------+
| id |    purchase_date    | asset |    size    | filled_price |     exchange      |  cost   |
+----+---------------------+-------+------------+--------------+-------------------+---------+
| 1  | 2021-02-20T17:52:49 |  ETH  | 2.05       |   1921.91    |   Coinbase Pro    |  3939.92|
| 2  | 2021-02-22T22:55:15 |  ETH  | 3.794      |    1620.0    |   Coinbase Pro    | 6146.28 |
| 3  | 2021-04-17T22:27:45 |  BTC  | 0.9549     |   47356.46   |      BlockFi      | 45220.68|

I like to have totals at the end of the table like:我喜欢在表格末尾有总计,例如:

Total ETH: 5.844
Total BTC: 0.9549

Is this possible using native Pandas?这可以使用原生熊猫吗?

像这样?

 positions['TOTAL'] = positions.groupby('asset')['size'].transform('sum')

Here is what worked.这是有效的。

    asset_totals = _pd.DataFrame(positions.groupby('asset')
                                .agg(asset=('asset', min),
                                     total_size=('size', sum),
                                     total_cost=('cost', sum))
                                .sort_values('total_size', ascending=False))

Note, using asset=('asset', min) is sort of "hacky" as we taking the min of asset type.请注意,使用asset=('asset', min)有点“hacky”,因为我们采用了asset类型的最小值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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