I was given a file to practice pandas on, and was asked this question:
Q: Access the last 10 rows and the first two columns of the index dataframe.
So, I tried this code:
df = index[(index.tail(10)) & (index.iloc[:, :2])]
df
but it gave me an error. How do I access the last 10 rows and first two columns of a dataframe?
I expected to have the last 10 rows of the first two columns in a variable.
One way is to try with .iloc
df = index.iloc[-10: , :2]
-10:
takes last 10 rows. Note the negative and look more into how iloc (section Indexing both axes).:2
after comma takes first two column i.e. 0 and 1.