英文:
Cannot read column in csv file
问题
import pandas as pd
import numpy as np
df = pd.read_csv("name.csv")
df.head()
结果如下:
0 2018-01-01;100
1 2018-01-02;97
2 2018-01-03;65
3 2018-01-04;55
4 2018-01-05;33
结果未显示在列格式中(数据未被检测到)。我该怎么做才能检测到数据?我在Python方面是新手。
英文:
When I'm trying to do this code:
import pandas as pd
import numpy as np
df = pd.read_csv("name.csv")
df.head()
And the results are:
0 2018-01-01;100
1 2018-01-02;97
2 2018-01-03;65
3 2018-01-04;55
4 2018-01-05;33
The results doesnt show up in column format (Datas are not detected).
What should I do to make datas are detected?
I'm new in Python
答案1
得分: 3
这很可能是因为您的 "name.csv" 文件是以分号分隔的文件,而不是通常的逗号分隔文件。Pandas.read_csv 默认读取逗号分隔的文件(参见:https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html)。要更改此设置,请执行以下操作:
import pandas as pd
import numpy as np
df = pd.read_csv("name.csv", sep=';')
df.head()
英文:
This is most likely due to your "name.csv" being a semicolon-separated file, instead of the usual comma-separated. Pandas.read_csv defaults to reading files that are comma-separated (see also: https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html). To change this, do the following:
import pandas as pd
import numpy as np
df = pd.read_csv("name.csv", sep=';')
df.head()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论