【问题标题】:Pandas apply unidecode to several columnsPandas 将 unidecode 应用于几列
【发布时间】:2017-11-16 06:57:35
【问题描述】:

我正在尝试将两个熊猫系列的所有元素从熊猫数据框(不是 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)

【问题讨论】:

    标签: python pandas unidecoder


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 2019-01-09
      • 1970-01-01
      • 2019-06-04
      • 2020-12-09
      相关资源
      最近更新 更多