Ben Chuanlong Du's Blog

It is never too late to learn.

Slicing of Pandas Dataframes

In [1]:
import pandas as pd
In [3]:
df = pd.DataFrame({"x": [1, 2, 3, 4, 5], "y": [5, 4, 3, 2, 1], "z": [1, 1, 1, 1, 1]})

df
Out[3]:
x y z
0 1 5 1
1 2 4 1
2 3 3 1
3 4 2 1
4 5 1 1

DataFrame.iloc

Ranges outside the range of rows/columns is allowed, in which case the existing part of the DataFrame is returned.

In [4]:
df.iloc[
    3:10,
]
Out[4]:
x y z
3 4 2 1
4 5 1 1
In [ ]:

Comments