How to skiprows across two different ranges in pandas

Viewed 1589

I want to isolate a data set that that is in the middle of a cvs file, but I don't know how.

I've tried

data_df_2 = pd.read_csv('data_set.csv', skiprows=range(1:8,21:114)

and

data_df_2 = pd.read_csv('data_set.csv', skiprows=range(1,8), skiprows=range(21,114)

and neither of them work. What is the correct way to approach my problem?

2 Answers

The pandas.read_csv doc states that skiprows need to be list-like, int or callable. Therefore, you can create a list comprehension to generate the ranges that you want to exclude.

pd.read_csv('data_set.csv', skiprows=([i for i in range(1, 8)] + [j for j in range(21, 114)]))

I changed your values for easy display purposes, but concept is the same.

skip_1 = list(range(1, 8, 1))
skip_2 = list(range(12, 15, 1))
skip_idxs_list = skip_1 + skip_2
df_slice = df.iloc[~df.index.isin(skip_idxs_list)]
df_slice.head(20)

enter image description here

Related