Dataframe encoding

Is there a way to encode my framework's index? I have a dataframe where the index is the name of international conferences.

df2= pd.DataFrame(index=df_conf['Conference'], columns=['Citation1991','Citation1992'])

I keep getting: KeyError: 'Leitf\xc3\xa4den der angewandten Informatik'

whenever my code refers to a foreign conference name with unknown ascii letters.

I tried:

df.at[x.encode("utf-8"), 'col1']

df.at[x.encode('ascii', 'ignore'), 'col']

      

Is there a way? I tried to figure out if I could code the framework myself when building it, but I can't seem to do that either.

+3


source to share


2 answers


If you are not using csv and want to encode your string index, this is what worked for me:



df.index = df.index.str.encode('utf-8')

      

+5


source


The encoding setting should be processed when reading the input file using the option encoding

df = pd.read_csv('bibliography.csv', delimiter=',', encoding="utf-8")

      



or if the file uses BOM

,

df = pd.read_csv('bibliography.csv', delimiter=',', encoding="utf-8-sig")

      

+8


source







All Articles