Pandas将unidecode应用于多个列

时间:2017-06-14 08:28:40

标签: python pandas unidecoder

我正在尝试将两个pandas系列的所有元素从pandas数据框转换为ascii字符,而不是ascii字符。只需将函数应用于相关列即可。 Python仅显示属性错误,指出'series'对象没有属性编码。

import pandas as pd 
import numpy as np
from unidecode import unidecode

try_data=pd.DataFrame({ 

 'Units': np.array([3,4,5,6,10],dtype='int32'),
 'Description_PD': pd.Categorical(['VEIJA 5 TRIÂNGULOS 200','QUEIJO BOLA','QJ BOLA GRD','VEIJO A VACA TRIÂNGULOS 100','HEITE GORDO TERRA']), 
 'Description_Externa' : pd.Categorical(['SQP 4 porções', 'Bola', ' SIESTA BOLA', 'SQP 16 porções', 'TERRA NOSTRA'])

     })

  try_data[['Description_PD','Description_Externa']].apply(unidecode)

1 个答案:

答案 0 :(得分:4)

迭代col列表并在循环调用apply中,由于某种原因你的尝试不起作用,但应该有:

In[47]:
for col in ['Description_PD','Description_Externa']:
    try_data[col] = try_data[col].apply(unidecode)
try_data

Out[47]: 
  Description_Externa               Description_PD  Units
0       SQP 4 porcoes       VEIJA 5 TRIANGULOS 200      3
1                Bola                  QUEIJO BOLA      4
2         SIESTA BOLA                  QJ BOLA GRD      5
3      SQP 16 porcoes  VEIJO A VACA TRIANGULOS 100      6
4        TERRA NOSTRA            HEITE GORDO TERRA     10

例如,在单个列上调用apply可以正常工作:

In[49]:
try_data['Description_Externa'].apply(unidecode)

Out[49]: 
0     SQP 4 porcoes
1              Bola
2       SIESTA BOLA
3    SQP 16 porcoes
4      TERRA NOSTRA
Name: Description_Externa, dtype: category
Categories (5, object): [SIESTA BOLA, Bola, SQP 16 porcoes, SQP 4 porcoes, TERRA NOSTRA]