Search code examples
pythonpandasrows

select a range of specific rows with pandas


I have an ascii file with two columns and 365 rows. A sample is given below. following

1   255.45833333333334
2   261.5833333333333
3   315.0416666666667
4   325.0833333333333
5   303.625
6   273.8333333333333
7   279.5416666666667
8   255.58333333333334
9   197.54166666666666
10  222.625
11  276.6666666666667
12  319.625
13  256.375

I want to read specific rows of the file. For example, I want to read the rows 1 to 5 and 10 to 13 and get the output. How I could do that with iloc? In case I want to extract consecutive rows I do the following. But what to do for a range of rows where I have to skip rows?

 df=pd.read_csv(file,sep='\t')
 df1=df.iloc[8:11]

Solution

  • Try something like:

    df.iloc[[*range(1, 5), *range(10, 13)]]