3 回答
TA贡献1868条经验 获得超4个赞
如注释中所述,您可以使用.str.contains(注意regex=False, 不将字符串视为正则表达式):
df = pd.DataFrame({'file_name': ['DIGITAL_SOURCE', 'Other1', 'Other3']})
df['contains'] = df['file_name'].str.contains('DIGITAL_SOURCE', regex=False).astype(int)
print(df)
印刷:
file_name contains
0 DIGITAL_SOURCE 1
1 Other1 0
2 Other3 0
TA贡献1869条经验 获得超4个赞
你应该做isin
np.where( df['file_name'].isin(['DIGITAL_SOURCE']), 1, 0) #df['file_name'].isin(['DIGITAL_SOURCE']).astype(int)
TA贡献1818条经验 获得超11个赞
您还可以应用 lambda,这样:
df['new_column'] = df.apply(lambda x: 1 if 'DIGITAL_SOURCE' in x['file_name'] else 0, axis=1 )
例子:
df = pd.DataFrame({"LOCATION":["USA","USA","USA","USA","JAPAN","JAPAN"],"file_name":["DIGITAL","DIGITAL","DIGITAL","DIGITAL","DIGITAL_SOURCE","DIGITAL_SOURCE"]})
LOCATION file_name
0 USA DIGITAL
1 USA DIGITAL
2 USA DIGITAL
3 USA DIGITAL
4 JAPAN DIGITAL_SOURCE
5 JAPAN DIGITAL_SOURCE
df['new_cl'] = df.apply(lambda x: 1 if 'DIGITAL_SOURCE' in x['file_name'] else 0, axis=1 )
LOCATION file_name new_cl
0 USA DIGITAL 0
1 USA DIGITAL 0
2 USA DIGITAL 0
3 USA DIGITAL 0
4 JAPAN DIGITAL_SOURCE 1
5 JAPAN DIGITAL_SOURCE 1
添加回答
举报