haley chen
haley chen

Reputation: 26

df.set_index returns key error python pandas dataframe

I have this Pandas DataFrame and I have to convert some of the items into coordinates, (meaning they have to be floats) and it includes the indexes while trying to convert them into floats. So I tried to set the indexes to the first thing in the DataFrame but it doesn't work. I wonder if it has anything to do with the fact that it is a part of the whole DataFrame, only the section that is "Latitude" and "Longitude".

df = df_volc.iloc(axis = 0)[0:, 3:5]

df.set_index("hello", inplace = True, drop = True) df

and I get the a really long error, but this is the last part of it: KeyError: '34.50'

if I don't do the set_index part I get:

Latitude Longitude 0 34.50 131.60 1 -23.30 -67.62 2 14.50 -90.88

I just wanna know if its possible to get rid of the indexes or set them.

Upvotes: 0

Views: 1797

Answers (1)

Ovi
Ovi

Reputation: 33

The parameter you need to pass to set_index() function is keys : column label or list of column labels / arrays. In your scenario, it seems like "hello" is not a column name.

I just wanna know if its possible to get rid of the indexes or set them.

It is possible to replace the 0, 1, 2 index with something else, though it doesn't sound like it's necessary for your end goal:

to convert some of the items into [...] floats

To achieve this, you could overwrite the existing values by using astype():
df['Latitude'] = df['Latitude'].astype('float')

Upvotes: 1

Related Questions