Python Pandas read_csv跳过行,但保留头 [英] Python Pandas read_csv skip rows but keep header

查看:6405
本文介绍了Python Pandas read_csv跳过行,但保留头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何在csv文件中跳过n行,但保留为1行的标题。

I'm having trouble figuring out how to skip n rows in a csv file but keep the header which is the 1 row.

我想要做的是迭代,但保留第一行的标题。 skiprows 使标题在跳过的行后面的第一行。这是最好的方法是什么?

What I want to do is iterate but keep the header from the first row. skiprows makes the header the first row after the skipped rows. What is the best way of doing this?

data = pd.read_csv('test.csv', sep='|', header=0, skiprows=10, nrows=10)


推荐答案

p>您可以将行号列表传递给 skiprows ,而不是整数。然后读者将忽略列表中的那些行。

You can pass a list of row numbers to skiprows instead of an integer. The reader will then ignore those rows in the list.

通过给出函数的整数10,你只是跳过前10行。

By giving the function the integer 10, you're just skipping the first 10 lines.

要保留第一行0(作为标题),然后跳到第10行,您可以这样写:

To keep the first row 0 (as the header) and then skip to row 10, you could write:

pd.read_csv('test.csv', sep='|', skiprows=range(1, 10))

这篇关于Python Pandas read_csv跳过行,但保留头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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