2

I have a dataframe that contains the following,

                   Reading
Date                        
2020-05-09 14:54:00    13661
2020-05-09 14:55:00    13672
2020-05-09 14:56:00    14251
2020-05-09 14:59:00    15255

I have figured out how to extract the last element:

lastelement=(series["Reading"].iloc[-1])

which gives me "15255"

Note:the first column is the index not a column in the dataframe as such.

so I also want to extract the index value for that same element (which may not be unique int the readings column), i.e. "2020-05-09 14:59:00"

I can anybody help?

thanks

(forgive me if my question is poor, its my first post on stackoverflow! - I can usually work my answers out from all of the other questions/answers presented here)

4 Answers 4

2

You can simply use df.tail():

df.tail(1)

If you only want the index value, you can add:

df.tail(1).index.values
Sign up to request clarification or add additional context in comments.

Comments

1

You can try the following code:-

data = [['2020-05-09 14:54:00', 13661],
        ['2020-05-09 14:55:00', 15255],
        ['2020-05-09 14:55:00', 13672],
        ['2020-05-09 14:56:00', 15255],
        ['2020-05-09 14:56:00', 14251],
        ['2020-05-09 14:59:00', 15255]]
df = pd.DataFrame(data, columns=["Date","Reading"])
df.set_index("Date",inplace=True)
last_element=df.Reading[len(df)-1]
last_element_indices = list(df[df["Reading"]==last_element].index.tolist())
print("Last Element :",last_element)
print("Indices :", last_element_indices)

Expected Output:-

Last Element : 15255
Indices : ['2020-05-09 14:55:00', '2020-05-09 14:56:00', '2020-05-09 14:59:00']

Hope this will solve your problem.

Comments

0

Compare the dataframe to the filtered value, squeeze it to get a boolean array, and index the index to get your values :

df.index[df.eq(df.iloc[-1]).squeeze()]

Index(['2020-05-09 14:55:00', '2020-05-09 14:56:00', '2020-05-09 14:59:00'], dtype='object', name='Date')

Comments

-1

For reading and index exact value.

Try:

df.tail(1).Reading.values[0]
df.tail(1).Date.values[0]

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.