简体   繁体   中英

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.

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. 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. If you want to skip single lines explicitly by line number (0 indexed), you must supply a list-like argument.

In your specific case, that would be 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'.

Hope it helps for similar problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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