pythonpandasdataframe

How to access the last 10 rows and first two columns of a DataFrame


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.


Solution

  • One way is to try with .iloc

    df = index.iloc[-10: , :2]