简体   繁体   English

Pandas dataframe,如何创建一个新的总计列,其中包含基于其他列的值

[英]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.如果TypeBalance Sheet ,我希望使用Total列 - 如果TypeFund ,则使用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.

相关问题 如何根据 pandas dataframe 中其他列中的子字符串创建新列? - How to create new column based on substrings in other column in a pandas dataframe? Pandas:根据我的 dataframe 中的其他值列表创建一个新列 - Pandas: Create a new column based on a list of other values in my dataframe 根据其他列中的“NaN”值在 Pandas Dataframe 中创建一个新列 - Create a new column in Pandas Dataframe based on the 'NaN' values in other columns 如何创建新的 pandas dataframe 列,其中包含所有其他列的值作为张量? - How to create new pandas dataframe column containing values of all other columns as a tensor? MultiIndex DataFrame:如何基于其他列中的值创建新列? - MultiIndex DataFrame: How to create a new column based on values in other column? 如何在 pandas dataframe 中创建新的总计(sumif)列? - How to create a new totals (sumif) column in a pandas dataframe? 基于数据框的其他列创建一个新的熊猫数据框列 - Create a new pandas dataframe column based on other column of the dataframe 如何根据 Pandas 数据框中的其他行创建新列? - How create a new column based on other rows in pandas dataframe? Pandas:根据 DataFrame 中的其他列在 DataFrame 中创建新列 - Pandas: Create new column in DataFrame based on other column in DataFrame 如何根据 Pandas DataFrame 中其他列的值创建新列 - How to create a new column based on values from other columns in a Pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM