Roman
Roman

Reputation: 131198

How to create a pandas dataframe containing columns with special characters?

I try to create a Pandas data-frame from a list of dictionaries:

df = pandas.DataFrame(ls, columns = cols)

As a result I get the following error message:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6: ordinal not in range(128)

I assume that the errors is caused by the fact that some values of the dictionary contains "special characters" (like ä or ö).

How can I make pandas to accept these characters?

Upvotes: 0

Views: 1216

Answers (1)

cge
cge

Reputation: 9890

You need to ensure that your default encoding is set to unicode; it is ascii by default. Try

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

Upvotes: 1

Related Questions