Pandas 将 unidecode 应用于多个列

Pandas apply unidecode to several columns

我正在尝试将 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)

遍历 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]