0

Here is the DataFrame I am working with, for reference.

data2 = {'col10':[1.0, 2.0, 3.0, 4.0], 'col11':[100, 200, 300, 400]}
df = pd.DataFrame(data2, index = ['a', 'b', 'c', 'd'])

I'm trying to create a new DataFrame, df2, from the last two rows of this column. I'm not sure how to do this and would appreciate some tips.

1 Answer 1

1

Try this:

newdf=df.loc[['c','d']]

>>> print(newdf)
   col10  col11
c    3.0    300
d    4.0    400

If you want a more generic solution, for example if you don;t know the index of your last tow rows, you can use the following:

newdf=df.iloc[-2:]

>>> print(newdf)
   col10  col11
c    3.0    300
d    4.0    400
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.