【发布时间】:2018-06-11 15:17:25
【问题描述】:
我应该如何根据条件将 NaN 值转换为分类值。尝试转换 Nan 值时出现错误。
category gender sub-category title
health&beauty NaN makeup lipbalm
health&beauty women makeup lipstick
NaN NaN NaN lipgloss
我的 DataFrame 看起来像这样。我将性别中的 NaN 值转换为分类值的函数看起来像
def impute_gender(cols):
category=cols[0]
sub_category=cols[2]
gender=cols[1]
title=cols[3]
if title.str.contains('Lip') and gender.isnull==True:
return 'women'
df[['category','gender','sub_category','title']].apply(impute_gender,axis=1)
如果我运行代码会出错
----> 7 if title.str.contains('Lip') and gender.isnull()==True:
8 print(gender)
9
AttributeError: ("'str' object has no attribute 'str'", 'occurred at index category')
【问题讨论】:
-
你认为
title.str是什么? -
isnull不是一个接受数组的函数吗? The docs。我不确定gender.isnull==True是否有意义, -
标题是熊猫系列。我正在该专栏中寻找 Lip
-
@LPR btw ,如果您喜欢其他答案,可以投票 :-)
标签: python pandas dataframe apply attributeerror