7

我正在尝试将两个熊猫系列的所有元素从熊猫数据框(不是 ascii 字符)转换为 ascii。简单地将函数应用于相关列是行不通的。Python 仅显示属性错误,指出“系列”对象没有属性编码。

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)
4

1 回答 1

13

遍历 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]
于 2017-06-14T08:35:39.267 回答