What is the suggested way to get, for example, the first X rows after an offset of Y? What I'm currently doing is:
offset, limit = 2, 2
df=pd.DataFrame([{'a':1}, {'a': 2}, {'a':3}, {'a': 4}, {'a':5}])
df.head(offset+limit)[offset:]
# a
# 2 3
# 3 4
Is there a better way to do this?
You can use df.iloc here.
df.iloc[offset: offset+limit]
a
2 3
3 4
There is closed GitHub issue which suggests using combination of head and tail
Something like this:
df.head(offset + limit).tail(limit)
This migh be bettet than accepted answer, cause due to docs iloc is "Deprecated since version 2.2.0"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With