pandas Python - AttributeError: 'DataFrame' 对象没有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50773258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:40:37  来源:igfitidea点击:

Python - AttributeError: 'DataFrame' object has no attribute

python-2.7pandascsvdataframe

提问by Rip_027

I have a CSV file with various columns and everything worked perfectly for the past few months until I updated the file and got new information and now the one column does not appear to be picked up by Python. I am using Python 2.7 and have made sure I have the latest version of pandas.

我有一个包含各种列的 CSV 文件,在过去的几个月里,一切都运行良好,直到我更新了文件并获得了新信息,现在 Python 似乎没有选择这一列。我正在使用 Python 2.7 并确保我拥有最新版本的 Pandas。

When I downloaded the csv file from Yahoo Finance, I opened it in Excel and made changes to the format of the columns in order to make it more readable as all information was in one cell. I used the "Text to Column" feature and split up the data based on where the commas were.

当我从雅虎财经下载 csv 文件时,我在 Excel 中打开它并更改了列的格式,以使其更具可读性,因为所有信息都在一个单元格中。我使用了“文本到列”功能并根据逗号所在的位置拆分数据。

Then I made sure that in each column there were no white spaces in the beginning of the cell using the Trim function in excel and left-aligning the data.

然后我使用 excel 中的 Trim 函数并左对齐数据确保在每一列中单元格的开头没有空格。

I tried the following and still get the same or similiar:

我尝试了以下操作,但仍然得到相同或相似的结果:

After the df = pd.read_csv("KIO.csv")I tried to read whether I can read the first few columns by using df.head()- but still got the same error.

df = pd.read_csv("KIO.csv")我尝试阅读是否可以通过使用读取前几列之后df.head()- 但仍然遇到相同的错误。

I tried renaming the problematic column as suggested in a similiar post using: df = df.rename(columns={"Close": "Closing"})- here I got the same error again. "print df.columns" also led to the same issue.

我尝试按照类似帖子中的建议重命名有问题的列: df = df.rename(columns={"Close": "Closing"})- 在这里我再次遇到相同的错误。“print df.columns”也导致了同样的问题。

"df[1]" - gave a long error with "KeyError: 1" at the end - I can print the entire thing if it it will assist.

df[1]” - 最后给出了“KeyError:1”的长错误 - 如果有帮助,我可以打印整个内容。

Adding the "skipinitialspace=True" - no difference.

添加“ skipinitialspace=True” - 没有区别。

I thought the problem might be within the actual csv file information so I deleted all the columns and made my own information and I still got the same error.

我认为问题可能出在实际的 csv 文件信息中,所以我删除了所有列并制作了自己的信息,但仍然出现相同的错误。

Below is a portion of my code as the total code is very long:

以下是我的代码的一部分,因为总代码很长:

enter code here
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as pltdate
import datetime
import matplotlib.animation as animation
import numpy as np

df = pd.read_csv("KIO.csv", skipinitialspace=True)
#df.head()

#Close = df.columns[0]
#df= df.rename(columns={"Close": "Closing"})
df1 = pd.read_csv("USD-ZAR.csv")

kio_close = pd.DataFrame(df.Close)
exchange = pd.DataFrame(df1.Value)
dates = df["Date"]
dates1 = df1["Date"]

The above variables have been used throughout the remaining code though so if this issue can be solved here the remaining code will be right.

上面的变量已经在剩余的代码中使用了,所以如果这个问题可以在这里解决,那么剩余的代码就是正确的。

This is copy/paste of the error:

这是错误的复制/粘贴:

Blockquote Traceback (most recent call last): File "C:/Users/User/Documents/PycharmProjects/Trading_GUI/GUI_testing.py", line 33, in kio_close = pd.DataFrame(df.Close) File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 4372, in getattrreturn object.getattribute(self, name) AttributeError: 'DataFrame' object has no attribute 'Close'

Blockquote Traceback(最近一次调用最后一次):文件“C:/Users/User/Documents/PycharmProjects/Trading_GUI/GUI_testing.py”,第 33 行,在 kio_close = pd.DataFrame(df.Close) 文件“C:\Python27\ lib\site-packages\pandas\core\generic.py”,第 4372 行,在getattr返回对象中。getattribute(self, name) AttributeError: 'DataFrame' 对象没有属性 'Close'

Thank you so much in advance.

非常感谢你提前。

采纳答案by Zoozoo

@Rip_027 This is in regards to your last comment. I used to have the same issue whenever I open a csv file by simply double clicking the file icon. You need to launch Excel first, then get external data. Link below has more details,which will serve as a guideline. Hope this helps.

@Rip_027 这是关于您最后的评论。每当我通过简单地双击文件图标打开 csv 文件时,我都会遇到同样的问题。您需要先启动 Excel,然后获取外部数据。下面的链接有更多详细信息,可作为指导。希望这可以帮助。

https://www.hesa.ac.uk/support/user-guides/import-csv