简体   繁体   English

基于列标题的 Pandas Dataframe 总和行

[英]Pandas Dataframe sum row based on column header

I have the following dataframe and want to create two columns, one will show the amount MTD and the other will show the cumulative YTD based on a date parameter for each Account Name.我有以下数据框并希望创建两列,一列将显示 MTD 金额,另一列将根据每个帐户名称的日期参数显示累计 YTD。 This is easily achievable in Excel using a =SUMIFS formula and want to know the Python equivalent.这很容易在 Excel 中使用 =SUMIFS 公式实现,并且想知道 Python 等价物。

+---------------+------------+------------+------------+------------+
| Account Names | 31/01/2022 | 28/02/2022 | 31/03/2022 | 30/04/2022 |
+---------------+------------+------------+------------+------------+
| Cash At Bank  |        100 |        150 |        100 |        150 |
| Debtors       |         50 |         50 |         50 |        100 |
| Inventory     |        250 |        250 |        350 |        100 |
| PAYG Withheld |         50 |         50 |         10 |        150 |
+---------------+------------+------------+------------+------------+

Ideally, I'd want this to be as efficient as possible ie doesn't require loops.理想情况下,我希望它尽可能高效,即不需要循环。 I went the route of trying to do this using np.select as I've read this is one of the fastest methods, but had no luck.我尝试使用 np.select 进行此操作,因为我读过这是最快的方法之一,但没有运气。 I get the following error:我收到以下错误:

ValueError: shape mismatch: objects cannot be broadcast to a single shape ValueError:形状不匹配:无法将对象广播到单个形状

EndDate = '31/03/2022'
Budget_Assets["MTD_Amount"] = np.select(condlist=[Budget_Assets.columns == EndDate],choicelist=[Budget_Assets[EndDate]],default=0)

For example, the value in the MTD_Amount column for Cash At Bank should be 100 and the YTD_Column will be 350 (sum of numbers from '31/01/2022' to '31/03/2022')例如,银行现金的 MTD_Amount 列中的值应为 100,YTD_Column 将为 350(从 '31/01/2022' 到 '31/03/2022' 的数字总和)

You can try sum(axis=1) by slicing the datetime like columns to calculate YTD and just use loc to get MTD您可以尝试sum(axis=1)通过像列一样切片日期时间来计算YTD并使用loc来获取MTD

EndDate = '31/03/2022'
date_cols = df.filter(regex='\d{2}/\d{2}/\d{4}')
date_cols.columns = pd.to_datetime(date_cols.columns, dayfirst=True)

df['YTD_Column'] = date_cols.loc[:, :pd.to_datetime(EndDate, dayfirst=True)].sum(axis=1)
df['MTD_Column'] = df[EndDate]
   Account Names  31/01/2022  28/02/2022  31/03/2022  30/04/2022  YTD_Column  MTD_Column
0   Cash At Bank         100         150         100         150         350         100
1        Debtors          50          50          50         100         150          50
2      Inventory         250         250         350         100         850         350
3  PAYG Withheld          50          50          10         150         110          10

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

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