Okorimi Manoury
Okorimi Manoury

Reputation: 134

how to decode colnames pandas dataframe with python?

I imported a data frame in python with pandas. But I have column names with strange encoding.

colnames = ['Price \xe2\x82\xac', 'x-rate \xe2\x82\xac/$']

Can you help me to decode these column names?

Upvotes: 2

Views: 1044

Answers (1)

rahlf23
rahlf23

Reputation: 9019

Try the following:

colnames = [i.encode('raw_unicode_escape').decode('utf-8') for i in colnames]

Yields:

['Price €', 'x-rate €/$']

Per @piRSquared's comment, you can do this with pandas using:

df.rename(columns=lambda x: x.encode('raw_unicode_escape').decode())

Upvotes: 5

Related Questions