尝试创建循环遍历元组列表的函数,并将每个新列附加到原始DataFrame。目前,该函数一次允许一个元组列表,并且我很难在我创建的函数中循环遍历所有元组列表。以下是我到目前为止的情况。谢谢你的帮助!
def divide_two_cols(df,tuple_list):
for x,y,z in tuple_list:
calc = df[x] /df[y]
calc.replace(np.inf, 0, inplace=True)
calc.fillna(value = 0, inplace = True)
df[str(z)] = calc
return df
tuple_list_2 = [('Total_Revenue', 'Spend', 'ROAS')]
tuple_list_3 = [('Total_Revenue', 'Conversions_10D', 'AOS')]
tuple_list_4 = [('Spend', 'Clicks', 'CPC')]
tuple_list_5 = [('Conversions_10D', 'Impressions', 'conv_rate')]
tuple_list_6 = [('Spend', 'Conversions_10D', 'CPA')]
tuple_list_7 = [('Clicks', 'Impressions', 'CTR')]
答案 0 :(得分:1)
def divide_two_cols(df,tuple_list):
for x,y,z in tuple_list:
calc = df[x] /df[y]
calc.replace(np.inf, 0, inplace=True)
calc.fillna(value = 0, inplace = True)
df[str(z)] = calc
return df
divide_two_cols(df, tuple_list_2 + tuple_list_3 + tuple_list_4 + ...)