0

I think I miss something very basic in the Pandas DataFrame:

I have the following program:


array = {'A':[1, 2, 3, 4], 'B':[5, 6, 7, 8]}
index = pd.DatetimeIndex(
    [   '09:30',
        '09:31',
        '09:32',
        '09:33'     ])
data = pd.DataFrame(array, index=index)
data.index = data.index.strftime('%H:%M')
         
print(data)
print(data.loc('09:33'))

I get:

       A  B
09:30  1  5
09:31  2  6
09:32  3  7
09:33  4  8

Which is great, but I can not access a row using it's index '09:33' and I get:

ValueError: No axis named 09:33 for object type <class 'pandas.core.frame.DataFrame'>

What am I missing ?

Thank you, Ehood

2 Answers 2

1

You need to use brackets [] instead: data.loc['09:33']

array = {'A':[1, 2, 3, 4], 'B':[5, 6, 7, 8]}
index = pd.DatetimeIndex(
    [   '09:30',
        '09:31',
        '09:32',
        '09:33'     ])
data = pd.DataFrame(array, index=index)
data.index = data.index.strftime('%H:%M')
         
print(data)
print(data.loc['09:33'])  # HERE!

Output:

       A  B
09:30  1  5
09:31  2  6
09:32  3  7
09:33  4  8
A    4
B    8
Name: 09:33, dtype: int64
Sign up to request clarification or add additional context in comments.

Comments

0

You're close. When using loc or iloc, its followed by square brackets, like this:

df.loc['viper']

In your case that ends up like this:

enter image description here

You can find the documentation for that here.

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.