簡體   English   中英

'DataFrame' object 沒有屬性

[英]'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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM