【发布时间】:2017-06-27 09:16:34
【问题描述】:
我需要对 pandas 执行的一项常见操作是从 Excel 文件中读取表格,然后从所有字段中删除分号。这些列通常是混合数据类型,我在尝试执行以下操作时遇到了 AtributeError:
for col in cols_to_check:
df[col] = df[col].map(lambda x: x.replace(';',''))
AttributeError: 'float' 对象没有属性 'replace'
当我在替换之前将其包装在 str() 中时,我遇到了 Unicode 字符问题,例如
for col in cols_to_check:
df[col] = df[col].map(lambda x: str(x).replace(';',''))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)
在 excel 中这是一个非常简单的操作,只需将; 替换为空字符串即可。如何在 pandas 中对整个数据框进行类似的操作,而不考虑数据类型?还是我错过了什么?
【问题讨论】:
标签: python string pandas replace