简体   繁体   English

在python中读取csv文件时跳过第二行数据帧

[英]skip second row of dataframe while reading csv file in python

I am unable to skip the second row of a data file while reading a csv file in python.在 python 中读取 csv 文件时,我无法跳过数据文件的第二行。

I am using the following code :我正在使用以下代码:

imdb_data = pd.read_csv('IMDB_data.csv', encoding = "ISO-8859-1",skiprows = 2) 

Your code will ommit the first two lines of your csv. 您的代码将省略csv的前两行。 If you want the second line to be ommitted (but the first one included) just do this minor change: 如果要省略第二行(但包括第一行),请进行以下较小更改:

imdb_data = pd.read_csv('IMDB_data.csv', encoding = "ISO-8859-1",skiprows = [1]) 

Looking at the documentation we can learn that if you supply an integer n for skiprows , the first n rows are skipped. 查看文档,我们可以了解到,如果为跳过行提供整数nskiprows跳过前n行。 If you want to skip single lines explicitly by line number (0 indexed), you must supply a list-like argument. 如果要按行号(索引为0)显式跳过单行,则必须提供类似列表的参数。

In your specific case, that would be skiprows=[1] . 在您的特定情况下,这将是skiprows=[1]

The question has already answered.问题已经回答了。 If one wants to skip number of rows at once, one can do the following:如果想一次跳过几行,可以执行以下操作:

df = pd.read_csv("transaction_activity.csv", skiprows=list(np.arange(1, 13)))

It will skip rows from second up to 12 by keeping your original columns in the dataframe, as it is counted '0'.通过将原始列保留在数据框中,它将从秒跳过行到 12,因为它被计数为“0”。

Hope it helps for similar problem.希望对类似问题有所帮助。

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

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