如何解决AttributeError:'NoneType'对象没有属性'head'

时间:2019-11-13 04:36:27

标签: python pandas

我正在使用Google,Apple和Amazon的股票数据。所有股票数据均以CSV格式从雅虎财经下载。我有一个包含Google股票数据的名为GOOG.csv的文件,一个包含苹果股票数据的名为AAPL.csv的文件,以及一个包含亚马逊股票数据的名为AMZN.csv的文件。尝试检查数据帧的输出时出现错误。

google_stock = google_stock.rename(columns={'Adj Close':'google_stock'},inplace=True)

# Change the Adj Close column label to Apple
apple_stock = apple_stock.rename(columns={'Adj Close':'apple_stock'},inplace=True)

# Change the Adj Close column label to Amazon
amazon_stock = amazon_stock.rename(columns={'Adj Close':'amazon_stock'},inplace=True)

google_stock.head()```


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-29-b562791246eb> in <module>()
      1 # We display the google_stock DataFrame
----> 2 google_stock.head()

AttributeError: 'NoneType' object has no attribute 'head'

1 个答案:

答案 0 :(得分:2)

inplace=True使它无需分配即可更新,因此可以使用:

google_stock.rename(columns={'Adj Close':'google_stock'},inplace=True)

# Change the Adj Close column label to Apple
apple_stock.rename(columns={'Adj Close':'apple_stock'},inplace=True)

# Change the Adj Close column label to Amazon
amazon_stock.rename(columns={'Adj Close':'amazon_stock'},inplace=True)

google_stock.head()

或使用不带inplace=True的普通分配:

google_stock = google_stock.rename(columns={'Adj Close':'google_stock'})

# Change the Adj Close column label to Apple
apple_stock = apple_stock.rename(columns={'Adj Close':'apple_stock'})

# Change the Adj Close column label to Amazon
amazon_stock = amazon_stock.rename(columns={'Adj Close':'amazon_stock'})

google_stock.head()