我有一个数据框,我想将函数传递给特定的列:
希望改变频道'基于某些要求的列值:
if 'Facebook' or 'FacebookPaid' or 'FacebookFree' in df['utm_Campaign']:
df['Channel'] = 'FB'
elif 'Newsletter' in df['utm_Campaign']:
df['Channel'] = 'Email'
else:
if 'YoutubePaid' in df['utm_Campaign']:
df['Channel'] = 'Youtube Direct'
基本上如果有一个单词' Facebook'包含在同一行的另一列(utm_Campaign)中,' Channel'中的字符串数据框的列设置为' FB'。
不确定该怎么做。我是映射函数,还是执行df.replace或其他?
答案 0 :(得分:1)
您可以按str.contains
创建条件,然后使用numpy.select
:
override func didSelectItem(at index: Int) {
// This never gets called
}
答案 1 :(得分:0)
您也可以这样做:
df['Channel'] = ['FB' if 'Face' in row else df.iloc[i].Channel for i,row in enumerate(df.utm_Campaign)]
答案 2 :(得分:0)
以防您想使用apply:
df['Channel'] = df.apply(lambda df: \
'FB' if df['utm_Campaign'].str.contains('Facebook') else \
('Email' if df['utm_Campaign'].str.contains('Newsletter') else \
('Youtube Direct' if df['utm_Campaign'].str.contains('YoutubePaid'))))