【问题标题】:Having difficulty dropping multiple columns from a Pandas dataframe难以从 Pandas 数据框中删除多列
【发布时间】:2019-10-29 16:22:31
【问题描述】:

我有一个新创建的 Pandas 数据框,由于某种原因我无法从中删除列。

RangeIndex:2617 个条目,0 到 2616 列:111 个条目,访问分区 数据类型:对象(111) 内存使用:2.2+ MB 无

print list(df)

['access', 'age', 'attic', 'basement_type', 'bathrooms', 'bedrooms_total', 'buyer', 'buyer_city', 'buyer_country', 'buyer_postal_code', 'colisting_agent_id', 'construction ','csa_number','date_expired_hidden','date_listed','date_sold','date_unconditional','distance_to_schools','distance_to_transportation','district','ensuites','exterior_features','exterior_finish','fireplace_types', '壁炉'、'地板'、'燃料'、'供暖'、'id'、'interior_features'、'internet_remarks']

df = df.drop(['buyer', 'buyer_city', 'buyer_country', 'colisting_agent_id'], axis=1, inplace=True)


print list(df)```



TypeErrorTraceback (most recent call last)
<ipython-input-63-1db0e7488634> in <module>()
      6 
      7 
----> 8 print list(df)

TypeError: 'NoneType' object is not iterable

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    inplace=True 参数将在源(即数据框本身)处丢弃并返回 None

    因此,当您使用 inplace=True 时,请不要将其分配回 df

    这里的错误是因为 df 变量被分配给 None 并且在 None 上调用 list 构造函数会引发错误。

    >>> df.drop(list_of_cols_to_drop, axis=1, inplace=True)
    

    【讨论】:

    • 完全有道理!谢谢
    【解决方案2】:

    你应该这样做:

    df = df.drop(['buyer', 'buyer_city', 'buyer_country'], axis=1)
    

    或者:

    df.drop(['buyer', 'buyer_city', 'buyer_country'], axis=1, inplace=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 2017-09-10
      • 2019-06-07
      • 2022-07-15
      相关资源
      最近更新 更多