【问题标题】:Conditionally fill column values based on another columns value in pandas根据熊猫中的另一个列值有条件地填充列值
【发布时间】:2012-05-29 17:45:55
【问题描述】:

我有一个 DataFrame 有几列。一列包含使用货币的符号,例如欧元或美元符号。另一列包含预算值。因此,例如,在一行中,它可能意味着 5000 欧元的预算,而在下一行中,它可能意味着 2000 美元的预算。

在 pandas 中,我想在我的 DataFrame 中添加一个额外的列,以欧元规范化预算。所以基本上,对于每一行,新列中的值应该是预算列中的值 * 1 如果货币列中的符号是欧元符号,新列中的值应该是预算列的值 *如果货币列中的符号是美元符号,则为 0.78125。

我知道如何添加一列、用值填充它、从另一列复制值等,但不知道如何根据另一列的值有条件地填充新列。

有什么建议吗?

【问题讨论】:

    标签: python python-3.x pandas dataframe numpy


    【解决方案1】:

    你可能想这样做

    df['Normalized'] = np.where(df['Currency'] == '$', df['Budget'] * 0.78125, df['Budget'])
    

    【讨论】:

    • 是否可以用文字而不是数字来做这样的事情?
    • df['Qnty'] = np.where(df['Quantity'].str.extract('([az]+)') == 'g', df['Quantity' ].str.extract('(\d+)').astype(int) / 1000, df['Quantity'].str.extract('(\d+)').astype(int)) 不知道任何人都需要这个,但我还是发布了。
    【解决方案2】:

    通过替代样式获得的类似结果可能是编写一个函数,该函数在一行上执行您想要的操作,使用 row['fieldname'] 语法访问单个值/列,然后对其执行 DataFrame.apply 方法

    这与此处链接的问题的答案相呼应:pandas create new column based on values from other columns

    def normalise_row(row):
        if row['Currency'] == '$'
        ...
        ...
        ...
        return result
    
    df['Normalized'] = df.apply(lambda row : normalise_row(row), axis=1) 
    

    【讨论】:

    • 应该是lambda row:normalise_row(row)?你不能用normalise_row 替换整个东西吗?
    【解决方案3】:

    进一步采用 Tom Kimber 的建议,您可以使用函数字典为您的函数设置各种条件。此解决方案正在扩大问题的范围。

    我正在使用来自个人应用程序的示例。

    # write the dictionary
    
    def applyCalculateSpend (df_name, cost_method_col, metric_col, rate_col, total_planned_col):
        calculations = {
                'CPMV'  : df_name[metric_col] / 1000 * df_name[rate_col],
                'Free'  : 0
                }
        df_method = df_name[cost_method_col]
        return calculations.get(df_method, "not in dict")
    
    # call the function inside a lambda
    
    test_df['spend'] = test_df.apply(lambda row: applyCalculateSpend(
    row,
    cost_method_col='cost method',
    metric_col='metric',
    rate_col='rate',
    total_planned_col='total planned'), axis = 1)
    
      cost method  metric  rate  total planned  spend
    0        CPMV    2000   100           1000  200.0
    1        CPMV    4000   100           1000  400.0
    4        Free       1     2              3    0.0
    

    【讨论】:

      【解决方案4】:

      不需要额外导入 numpy 的选项:

      df['Normalized'] = df['Budget'].where(df['Currency']=='$', df['Budget'] * 0.78125)
      

      【讨论】:

        猜你喜欢
        • 2019-09-09
        • 1970-01-01
        • 2020-11-26
        • 2019-04-28
        • 2019-06-05
        • 2021-09-03
        • 1970-01-01
        • 2019-05-04
        相关资源
        最近更新 更多