pandas 解析csv错误-预期找到1个字段9 [英] Pandas parsing csv error - expected 1 fields found 9

查看:46
本文介绍了 pandas 解析csv错误-预期找到1个字段9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从.csv文件进行解析:

I'm trying to parse from a .csv file:

planets = pd.read_csv("planets.csv", sep=',')

但是我总是以这个错误结尾:

But I always end up with this error:

ParserError: Error tokenizing data. C error: Expected 1 fields in line 13, saw 9

这是我的csv文件的前几行的样子:

This is how the first few lines of my csv file look like:

# This file was produced by the test
# Tue Apr  3 06:03:27 2018
#
# COLUMN pl_hostname:    Host Name
# COLUMN pl_discmethod:  Discovery Method
# COLUMN pl_pnum:        Number of Planets in System
# COLUMN pl_orbper:      Orbital Period [days]
# COLUMN pl_orbsmax:     Orbit Semi-Major Axis [AU])
# COLUMN st_dist:        Distance [pc]
# COLUMN st_teff:        Effective Temperature [K]
# COLUMN st_mass:        Stellar Mass [Solar mass] 
#
loc_rowid,pl_hostname,pl_discmethod,pl_pnum,pl_orbper,pl_orbsmax,st_dist,st_teff,st_mass
1,11 Com,Radial Velocity,1,326.03000000,1.290000,110.62,4742.00,2.70
2,11 UMi,Radial Velocity,1,516.22000000,1.540000,119.47,4340.00,1.80
3,14 And,Radial Velocity,1,185.84000000,0.830000,76.39,4813.00,2.20
4,14 Her,Radial Velocity,1,1773.40000000,2.770000,18.15,5311.00,0.90
5,16 Cyg B,Radial Velocity,1,798.50000000,1.681000,21.41,5674.00,0.99
6,18 Del,Radial Velocity,1,993.30000000,2.600000,73.10,4979.00,2.30
7,1RXS J160929.1-210524,Imaging,1,,330.000000,145.00,4060.00,0.85

这是第13行:

loc_rowid,pl_hostname,pl_discmethod,pl_pnum,pl_orbper,pl_orbsmax,st_dist,st_teff,st_mass

编辑:感谢@Rakesh,跳过前12行可以解决问题

Thanks to @Rakesh, Skipping the first 12 lines solved the problem

planets = pd.read_csv("planets.csv",sep =',',skiprows = 12)

planets = pd.read_csv("planets.csv", sep=',', skiprows=12)

推荐答案

函数 pandas.read_csv() 从第一行获取列数及其名称.默认情况下,它不将第一行的选项视为注释.

The function pandas.read_csv() gets the number of columns and their names from the first line. By default it does not consider the option of the first lines being comments.

正在发生的事情是,pandas读取了第一行,将其拆分,发现只有一列,将这一拆分插入到第13行,即第一条未注释行.为了解决这个问题,可以使用参数 comment .

What is happening is that pandas reads the first line, splits it and finds there is only one column, insetad of doing this split to the line 13 which is the first not commented line. To solve this, the argument comment can be used.

planets = pd.read_csv("planets.csv", comment='#')

与使用 skiprows 相比,即使注释行的数量有所不同,这也允许相同的代码加载 planets.csv 文件.

Compared to using skiprows, this allows the same code to load the planets.csv file even if the number of comment lines vary.

这篇关于 pandas 解析csv错误-预期找到1个字段9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆