pandas 将可选后缀添加到列名作为函数中的参数

pandas adding an optional suffix to a column name as a parameter in a function

Objective:我有一个 pandas 数据框,其中包含许多列名称,除了后缀外它们都是相同的。我希望能够通过参数传递后缀来引用函数中的列。 示例:


import pandas as pd
df_test = pd.DataFrame({'HHLD_ID':[1,2,3,4,5],
                       'sales':[25,50,25,25,50],
                       'sales_sk':[1,2,1,1,2],
                       'sales_rf':[1,2,1,1,5]
                       })

def total_print(df,label,suffix=''):  
    print(f'{label} Total dollars, {df[sales+suffix].sum()}')

total_print(df_test,"Your rf sales are :","_rf")
total_print(df_test,"Your sales are :")

预期答案
您的射频销量为:10
您的销量是:175

IIUC,你可以使用字符串 format:

import pandas as pd

df_test = pd.DataFrame({'HHLD_ID': [1, 2, 3, 4, 5],
                        'sales': [25, 50, 25, 25, 50],
                        'sales_sk': [1, 2, 1, 1, 2],
                        'sales_rf': [1, 2, 1, 1, 5]
                        })

sales = "sales{}".format


def total_print(df, label, suffix=''):
    print(f'{label} Total dollars, {df[sales(suffix)].sum()}')


total_print(df_test, "Your rf sales are :", "_rf")
total_print(df_test, "Your sales are :")

输出

Your rf sales are : Total dollars, 10
Your sales are : Total dollars, 175

作为替代方案,您也可以直接使用 f-strings

def total_print(df, label, suffix=''):
    print(f'{label} Total dollars, {df[f"sales{suffix}"].sum()}')