简体   繁体   English

在Python中创建可视化:EmptyDataError

[英]creating visualization in Python : EmptyDataError

I use python to read a csv file and create some figures: 我使用python读取csv文件并创建一些数字:

import csv
Teaching=open('A.csv','rb')
reader = csv.reader(Teaching)
#type is list.....
#create figure1,2,3 by using bokeh
#.....
import pandas as pd
df = pd.read_csv(Teaching)
#create figure4 by using bokeh
#I use series type to create a scatter plot

It has an error: EmptyDataError: No columns to parse from file 它有一个错误:EmptyDataError:没有要从文件中解析的列

EmptyDataError                            Traceback (most recent call last)
<ipython-input-45-c97e3d2be637> in <module>()
----> 1 df = pd.read_csv(Teaching)

If I reopen the CSV file, it will work and create a scatter plot 如果我重新打开CSV文件,它将起作用并创建散点图

Please tell me why, how to modify? 请告诉我为什么,如何修改? Thank you 谢谢

After you iterate the file object, you read to the end of the file, next time you want to read the data, it will return this error: 迭代file对象后,读取到文件的末尾,下次要读取数据时,它将返回此错误:

pandas.io.common.EmptyDataError: No columns to parse from file

So you can try to reopen it or use file.seek(0) to reposition to the start of the file. 因此,您可以尝试重新打开它或使用file.seek(0)重新定位到文件的开头。

The code should be like this: 代码应如下所示:

import csv
Teaching=open('a.csv','rb')
reader = csv.reader(Teaching)
for r in reader:
    print(r)

import pandas as pd
Teaching.seek(0)
df = pd.read_csv(Teaching)
print(df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM