【发布时间】:2021-08-06 21:03:03
【问题描述】:
我正在使用 python 2.7,并且我有一个带有希腊字母的数据框,如下所示:
units = ['Ab_alpha_cd', 'XY_beta_zz', 'Ef_gamma_gh']
descriptions = ['Name1 (Ab_alpha_cd): description_1', 'Name2 (XY_beta_zz): description_2', 'Name3 (Ef_gamma_gh): description_3'
df = pd.dataframe
df['units'] = units
df['descriptions'] = descriptions
我需要将描述栏中的'_greek_' 更改为正确的希腊字母。
我尝试了几种方法:
#first try:
for i in range (0, len(df)):
df.loc[i,'descriptions'].replace('_alfa_','\u03B1').replace('_beta_','\u03B2').replace('_gamma_','\u03B3')
#second try:
for i in range (0, len(df)):
df.loc[i,'descriptions'].replace('_alfa_',unichr(945)).replace('_beta_',unichr(946)).replace('_gamma_',unichr(947))
#thrid try
for i in range (0, len(df)):
df.loc[i,'descriptions'].replace('_alfa_','α').replace('_beta_','β').replace('_gamma_','γ')
#fourth try
for i in range (0, len(df)):
df.loc[i,'descriptions'].replace('_alfa_','α'.encode('utf-8')).replace('_beta_','β'.encode('utf-8')).replace('_gamma_','γ'.encode('utf-8'))
但没有任何效果。当我打电话给df.head() 时,希腊字母不显示。当我尝试将 df 写入 csv 文件时,它们也不会显示,即使使用 utf-8 编码也是如此。
我需要最终结果是一个字符串更正的列表。
我能做什么?
【问题讨论】:
-
当您尝试阅读
replace的文档时发生了什么? -
欢迎来到stackoverflow,请阅读tour和minimal reproducible example,在这种情况下还有:how-to-make-good-reproducible-pandas-examples (1)
标签: python python-2.7 utf-8