[英]'DataFrame' object has no attribute
我想使用 Python 3 构建推文分类器。我收到以下错误:
AttributeError: 'DataFrame' object 没有属性 'id'
当我运行以下代码时:
train_df['id'] = train_df.id.apply(lambda x: int(x))
train_df['friends_count'] = train_df.friends_count.apply(lambda x: int(x))
train_df['followers_count'] = train_df.followers_count.apply(lambda x: 0 if x=='None' else int(x))
train_df['friends_count'] = train_df.friends_count.apply(lambda x: 0 if x=='None' else int(x))
数据集看起来像
它是一个 csv 文件。 我想要一个数据集中所有列的列表,而不是手动滚动。 我检查了熊猫的版本,它似乎已经更新了。 我在使用 Python 方面很新,所以我希望你能帮助我找出我做错了什么。 谢谢
编辑:
AttributeError Traceback (most recent call last)
<ipython-input-32-13c1484f8b58> in <module>
2 train_df = df.copy()
3 #train_df['id'] = train_df.id.apply(lambda x: int(x))
----> 4 train_df['friends_count'] = train_df.friends_count.apply(lambda x: int(x))
5 train_df['followers_count'] = train_df.followers_count.apply(lambda x: 0 if x=='None' else int(x))
6 train_df['friends_count'] = train_df.friends_count.apply(lambda x: 0 if x=='None' else int(x))
/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
4374 IE10 404 0.08
4375 Chrome 200 0.02
-> 4376
4377 >>> df.reindex(new_index, fill_value='missing')
4378 http_status response_time
AttributeError: 'DataFrame' object has no attribute 'friends_count'
https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html
阅读这篇文章。
您需要更多地了解 pandas 以及它是如何工作的,然后才能对这个问题的答案有所帮助。
目前,您的列仅显示为0,1,2,...
。 您可能有兴趣将第一行用作列名。 您需要先通过以下方式将第一个数据行转换为列:
train_df.columns = train_df.iloc[0]
或者
train_df.rename(columns=train_df.iloc[0])
然后您将能够执行当前正在执行的操作。 您还可以通过以下方式删除当前 header 行:
train_df.drop(train_df.index[0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.