如何根据条件将NaN值转换为分类值.我在尝试转换Nan值时遇到错误.
category gender sub-category title
health&beauty NaN makeup lipbalm
health&beauty women makeup lipstick
NaN NaN NaN lipgloss
Run Code Online (Sandbox Code Playgroud)
我的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)
Run Code Online (Sandbox Code Playgroud)
如果我运行代码我会收到错误
----> 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')
Run Code Online (Sandbox Code Playgroud)