数据帧编码 [英] Dataframe encoding
问题描述
有没有一种方法可以对数据框的索引进行编码?我有一个数据框,其中的索引是国际会议的名称.
Is there a way to encode the index of my dataframe? I have a dataframe where the index is the name of international conferences.
df2= pd.DataFrame(index=df_conf['Conference'], columns=['Citation1991','Citation1992'])
我不断得到:
KeyError: 'Leitf\xc3\xa4den der angewandten Informatik'
只要我的代码引用带有未知ascii字母的外国会议名称.
whenever my code references a foreign conference name with unknown ascii letters.
我尝试过:
df.at[x.encode("utf-8"), 'col1']
df.at[x.encode('ascii', 'ignore'), 'col']
有办法解决吗?我试图查看我是否可以在创建数据帧时对其进行编码,但似乎也无法做到这一点.
Is there a way around it? I tried to see if I could encode the dataframe itself when creating it, but it doesn't seem I can do that either.
推荐答案
如果您不使用csv,并且想对字符串索引进行编码,那么这对我有用:
If you're not using csv, and you want to encode your string index, this is what worked for me:
df.index = df.index.str.encode('utf-8')
这篇关于数据帧编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!