Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyError: 0 when accessing value in pandas series

In my script I have df['Time'] as shown below.

497   2017-08-06 11:00:00
548   2017-08-08 15:00:00
580   2017-08-10 04:00:00
646   2017-08-12 23:00:00
Name: Time, dtype: datetime64[ns]

But when i do

t1=pd.Timestamp(df['Time'][0])

I get an error like this :

KeyError: 0

Do I need any type conversion here, if yes then how it can be fixed?

like image 244
Bharat Sharma Avatar asked Sep 11 '17 10:09

Bharat Sharma


People also ask

How do I fix pandas KeyError?

We can simply fix the error by correcting the spelling of the key. If we are not sure about the spelling we can simply print the list of all column names and crosscheck.

What does KeyError 0 mean in Python?

The Python "KeyError: 0" exception is caused when we try to access a 0 key in a a dictionary that doesn't contain the key. To solve the error, set the key in the dictionary before trying to access it or conditionally set it if it doesn't exist.

How do I fix KeyError in Python?

The Usual Solution: . If the KeyError is raised from a failed dictionary key lookup in your own code, you can use . get() to return either the value found at the specified key or a default value.

What is a KeyError in pandas?

Pandas KeyError. In most cases, think of 'key' as the same as 'name.' Pandas is telling you that it can not find your column name. The preferred method is to *make sure your column name is in your dataframe.*

How do you access elements in a pandas series?

Accessing elements of a Pandas Series. Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). Labels need not be unique but must be a hashable type. An element in the series can be accessed similarly to that in an ndarray.

Why is my Dataframe not opening in pandas?

This error occurs when you attempt to access some column in a pandas DataFrame that does not exist. Typically this error occurs when you simply misspell a column names or include an accidental space before or after the column name. The following example shows how to fix this error in practice.

What is a Python keyerror?

What a Python KeyError Usually Means A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary (dict). Python’s official documentation says that the KeyError is raised when a mapping key is accessed and isn’t found in the mapping. A mapping is a data structure that maps one set of values to another.

How do I fix a keyerror in a Dataframe?

Since there is no ‘point’ column in our DataFrame, we receive a KeyError. The way to fix this error is to simply make sure we spell the column name correctly.


1 Answers

You're looking for df.iloc.

df['Time'].iloc[0]

df['Time'][0] would've worked if your series had an index beginning from 0

And if need scalar only use Series.iat:

df['Time'].iat[0]
like image 163
cs95 Avatar answered Oct 11 '22 07:10

cs95