小编LPR*_*LPR的帖子

使用pandas时,为什么会出现AttributeError?

如何根据条件将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)

完整数据集 - https://github.com/lakshmipriya04/py-sample

python apply attributeerror dataframe pandas

7
推荐指数
2
解决办法
5880
查看次数

标签 统计

apply ×1

attributeerror ×1

dataframe ×1

pandas ×1

python ×1