从整个数据框中删除字符 [英] Removing a character from entire data frame

查看:73
本文介绍了从整个数据框中删除字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对熊猫执行的一项常见操作是从Excel文件中读取表格,然后从所有字段中删除分号.这些列通常是混合数据类型,尝试执行类似操作时会遇到AtributeError:

A common operation that I need to do with pandas is to read the table from an Excel file and then remove semicolons from all the fields. The columns are often in mixed data types and I run into AtributeError when trying to do something like this:

for col in cols_to_check:
    df[col] = df[col].map(lambda x: x.replace(';',''))

AttributeError:浮动"对象没有属性替换"

AttributeError: 'float' object has no attribute 'replace'

在替换之前将其包装在str()中时,我遇到了Unicode字符的问题,例如

when I wrap it in str() before replacing I have problems with Unicode characters, e.g.

for col in cols_to_check:
    df[col] = df[col].map(lambda x: str(x).replace(';',''))

UnicodeEncodeError:'ascii'编解码器无法对位置3中的字符u'\ xe9'进行编码:序数不在范围内(128)

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)

在excel中,这是一个非常简单的操作,只需将;替换为空字符串即可.无论数据类型如何,我如何在整个数据帧中以类似的方式在熊猫中执行此操作?还是我错过了什么?

In excel this is a very simple operation, all it takes is to replace ; with an empty string. How can I do it similarly in pandas for entire dataframe, disregard of data types? Or am I missing something?

推荐答案

您可以使用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆