我想使用Python 3构建推文的分类器。 我收到以下错误:
AttributeError:“ DataFrame”对象没有属性“ 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'
答案 0 :(得分:0)
https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html
阅读本文。
在回答这个问题之前,您需要了解更多有关熊猫的知识以及熊猫如何工作。
答案 1 :(得分:0)
当前,您的列仅显示为0,1,2,...
。您可能有兴趣将第一行用作列名。
您首先需要按照以下方式将第一行数据转换为列:
train_df.columns = train_df.iloc[0]
或
train_df.rename(columns=train_df.iloc[0])
然后,您将可以执行当前正在执行的操作。 您还可以通过以下方式删除当前标题行:
train_df.drop(train_df.index[0])