使用
in
apply
如果有
list
NaN
s添加
fillna
:
df["genres"] = df.genres.str.split("|")
df['new'] = df['genres'].fillna('').apply(lambda x: 'Comedy' in x)
print (df)
genres new
0 [Adventure, Animation, Children, Comedy, Fantasy] True
1 [Adventure, Children, Fantasy] False
2 [Comedy, Romance] True
3 [Comedy, Drama, Romance] True
4 [Comedy] True
5 NaN False
John Galt
对于解决方案:
df['new'] = ['Comedy' in x for x in df['genres']]
列表
使用
contains
带参数
na=False
:
df['new'] = df['genres'].str.contains('Comedy', na=False)
print (df)
genres new
0 Adventure|Animation|Children|Comedy|Fantasy True
1 Adventure|Children|Fantasy False
2 Comedy|Romance True
3 Comedy|Drama|Romance True
4 Comedy True
5 NaN False