无法读取CSV文件中的列。

huangapple go评论69阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年6月8日 16:07:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定