Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

I want to change a dataframes' index (rows) from float64 to string or Unicode.

I thought this would work but not:

#check type

type(df.index)

'pandas.core.index.Float64Index'

#change type to unicode

if not isinstance(df.index, unicode):

    df.index = df.index.astype(unicode)

error message:

TypeError: Setting <class 'pandas.core.index.Float64Index'> dtype to anything other than float64 or object is not supported

1 Answer

0 votes
by (107k points)

For Python 2, you can refer the following code as this works for me:

df.index = df.index.map(unicode) 

And for Python 3, the unicode type does not exist and is replaced by str. Refer the following code:

df.index = df.index.map(str)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...