3

I have a DataFrame looking like this:

             28    91    182
Date                        
2017-09-07  0.97  1.05  1.15
2017-09-08  0.95  1.04  1.14
2017-09-11  0.96  1.06  1.16
2017-09-12  0.99  1.04  1.16
2017-09-13  0.99  1.04  1.16

From this DataFrame i would like to get a list of the values of the last row.

[0.99, 1.04, 1.16]

I attempted to use

np.array(tbill.iloc[-1:].values).tolist()

which returns

[[0.99, 1.04, 1.16]]

but feels overly complicated.

Is there a more simple way to do this?

1
  • You could have done np.array(tbill.iloc[-1:].values).tolist()[0] (but answer below is better). Commented Sep 14, 2017 at 14:27

3 Answers 3

7

Just slice the underlying array.

df.values[-1].tolist()

which yields

[0.99, 1.04, 1.16]
Sign up to request clarification or add additional context in comments.

Comments

2

Or just:

df.iloc[-1].tolist()

Example:

df = pd.DataFrame(np.random.randn(10,3))

print(df.iloc[-1].tolist())
[-0.3000246004134489, -0.3626924316159151, 0.9523820239889618]

@miradulo's solution will actually be faster in this case, I believe because indexing a NumPy array is significantly faster than indexing a DataFrame.

Comments

2

There is a function called tail that you can access rows backward.

df.tail(1).values.tolist()   # get last row and its values

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.tail.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.