[英]Pandas dataframe, how to create a new totals column containing values based on other column
>>> df
Dr Name Type Total Fund Total
0 Debtors Balance Sheet 200.00 0.00
1 Bank Balance Sheet 352.25 100.00
4 General Fund Fund -100.00 -252.25
5 Building Fund Fund 0.00 -300.00
I want a new column to be created, being filled with either Totals or
Fund Totals.我想要创建一个新列,填充
Totals or
基金总计。
If the Type
is Balance Sheet
I would like the Total
column to be used - and if the Type
is Fund
the Fund Total
to be used.如果
Type
是Balance Sheet
,我希望使用Total
列 - 如果Type
是Fund
,则使用Fund Total
。 To produce this:要产生这个:
>>> df
Dr Name Type Total Fund Total Grand Total
0 Debtors Balance Sheet 200.00 0.00 200.00
1 Bank Balance Sheet 352.25 100.00 352.25
4 General Fund Fund -100.00 -252.25 -252.25
5 Building Fund Fund 0.00 -300.00 -300.00
Thanks谢谢
This is a possible solution:这是一个可能的解决方案:
df["Grand Total"] = df.where(df["Type"] == "Fund")["Fund Total"].fillna(df["Total"])
With df.where
I extract the column Fund Total
only where the type is Fund (other rows will be nan
. Then fillna
fills those nan
using another column ( Total
in this case)使用
df.where
我仅在类型为 Fund 的情况下提取列Fund Total
(其他行将是nan
。然后fillna
使用另一列填充那些nan
(在这种情况下为Total
)
Another way is to use where
from numpy
:另一种方法是使用
where
from numpy
:
import panda as pd
df['Grand Total'] = np.where(df['Type'] == 'Fund', df['Fund Total'], df['Total'])
print(df)
# Output
Dr Name Type Total Fund Total Grand Total
0 Debtors Balance Sheet 200.00 0.00 200.00
1 Bank Balance Sheet 352.25 100.00 352.25
4 General Fund Fund -100.00 -252.25 -252.25
5 Building Fund Fund 0.00 -300.00 -300.00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.