我有一个 Dataframe df2
,希望基于两个现有列上的if
逻辑生成一个名为“tag”的新列。
import pandas as pd
df2 = pd.DataFrame({'NOTES': ["PREPAID_HOME_SCREEN_MAMO","SCREEN_MAMO",
"> Unable to connect internet>4G Compatible>Set",
"No>Not Barred>Active>No>Available>Others>",
"Internet Not Working>>>Unable To Connect To"],
'col_1': ["voice", "voice","data","other","voice"],
'col_2': ["DATA", "voice","VOICE","VOICE","voice"]})
其逻辑和我的尝试是:
df2['Tag'] =
if df['col_1']=='data':
return "Yes"
elif df['col_2']:
return "Yes"
else:
return "No"
但是我得到了一个语法错误:
1条答案
按热度按时间zwghvu4y1#
问题是您试图使用if-语句赋值,这会导致语法错误。
有许多方法可以做到这一点,我提供了一个使用
pandas.DataFrame.apply
。