简体   繁体   English

Pandas read_csv函数正在读取csv头错误

[英]Pandas read_csv function is reading csv header wrong

See example csv file below: 请参阅下面的示例csv文件:

A,B,C 
d,e,f 
g,h,i 

The first row with the capital letters are my headings . 第一行用大写字母是我的标题

I tried this: 我试过这个:

df = pd.read_csv("example.csv", header=0, sep=",", index_col=0, parse_dates=True)

And the data frame that is created looks like this with the headings messed up. 并且创建的数据框看起来像这样,标题搞砸了。

  B C 
A 
d e f
g h i

Anyone know why or how I can fix this manually? 任何人都知道为什么或如何手动修复此问题?

The issue is that when you pass index_col=0 argument to read_csv() , it takes the 0th column as the index column, hence in your resulting DataFrame, A is the index. 问题是当您将index_col=0参数传递给read_csv() ,它将0th列作为索引列,因此在生成的DataFrame中, A是索引。

If you do not want to take A as the index, you should just omit the index_col=0 argument. 如果您不想将A作为索引,则应省略index_col=0参数。 Example - 示例 -

df = pd.read_csv("example.csv", parse_dates=True)

I removed some other keyword arguments as well - 我也删除了一些其他关键字参数 -

  • header=0 , header is 0 by default if names argument is not passed. header=0 ,如果未传递names参数,则标头默认为0。
  • sep=',' , seperator is ',' by default. sep=',' ,seperator默认为','

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

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