答案 0 :(得分:1)
You can use Series.str.decode() on the columns with the offending encoding,,但如果您可以重新读取数据并直接访问它,我不喜欢这种方法。
读取数据时可以使用encoding='utf-8'
参数,Pandas会尝试为您解决问题。假设您的数据在csv中且采用UTF-8编码,则类似这样:
df = pd.read_csv("yourfile.csv", encoding="utf-8")
编辑:您注意到您的数据是从数据库导入的,并且pandas.read_sql
没有encoding
arg。因此,我建议使用我的第一个建议Series.str.decode()
。您将在列上这样使用它:
df["column_name"] = df["column_name"].str.decode("encoding_name")
如果遇到错误,可以传递errors
,默认值是strict
,但也可以ignore
。
df["column_name"] = df["column_name"].str.decode("encoding_name", errors="policy")