在csv导入 pandas 期间跳过行 [英] Skip rows during csv import pandas

查看:71
本文介绍了在csv导入 pandas 期间跳过行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pandas.read_csv()导入.csv文件,但是我不想导入数据文件的第二行(索引为1的行用于0索引).

I'm trying to import a .csv file using pandas.read_csv(), however I don't want to import the 2nd row of the data file (the row with index = 1 for 0-indexing).

我看不到如何不导入它,因为与命令一起使用的参数似乎模棱两可:

I can't see how not to import it because the arguments used with the command seem ambiguous:

从熊猫网站:

skiprows:类似列表或整数

skiprows : list-like or integer

要跳过的行数(索引为0)或要跳过的行数(整数) 文件的开头."

Row numbers to skip (0-indexed) or number of rows to skip (int) at the start of the file."

如果我在参数中放入skiprows=1,它怎么知道是跳过第一行还是跳过索引为1的行?

If I put skiprows=1 in the arguments, how does it know whether to skip the first row or skip the row with index 1?

推荐答案

您可以尝试:

>>> import pandas as pd
>>> from StringIO import StringIO
>>> s = """1, 2
... 3, 4
... 5, 6"""
>>> pd.read_csv(StringIO(s), skiprows=[1], header=None)
   0  1
0  1  2
1  5  6
>>> pd.read_csv(StringIO(s), skiprows=1, header=None)
   0  1
0  3  4
1  5  6

这篇关于在csv导入 pandas 期间跳过行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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