Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 熊猫数据框取代国际货币符号_Python 2.7_Pandas_Unicode_Utf 8 - Fatal编程技术网

Python 2.7 熊猫数据框取代国际货币符号

Python 2.7 熊猫数据框取代国际货币符号,python-2.7,pandas,unicode,utf-8,Python 2.7,Pandas,Unicode,Utf 8,使用在多列中具有国际货币符号的excel文件。除此之外,还有一些国际语言 Example: Paying £40.50 doesn't make any sense for a one-hour parking. Example: Produkty są zbyt drogie (Polish) Example: 15% de la population féminine n'obtient pas de bons emplois (French) 作为清理过程,已采取以下措施 df = d

使用在多列中具有国际货币符号的excel文件。除此之外,还有一些国际语言

Example: Paying £40.50 doesn't make any sense for a one-hour parking. 
Example: Produkty są zbyt drogie (Polish)
Example: 15% de la population féminine n'obtient pas de bons emplois (French)
作为清理过程,已采取以下措施

df = df.apply(lambda x: x.str.replace('\\r',' '))
df = df.apply(lambda x: x.str.replace('\\n',' '))
df = df.apply(lambda x: x.str.replace('\.+', ''))
df = df.apply(lambda x: x.str.replace('-', ''))
df = df.apply(lambda x: x.str.replace('&', ''))
df = df.apply(lambda x: x.str.replace(r"[\"\',]", ''))
df = df.apply(lambda x: x.str.replace('[%*]', ''), axis=1)
(如果有更有效的方法-非常欢迎)

除此之外,还创建了:方法来删除停止字

def cleanup(row):
    stops = set(stopwords.words('english'))
    removedStopWords = " ".join([str(i) for i in row.lower().split() 
    return removedStopWords
要将此方法应用于包含上述示例的数据框中的所有列,请执行以下操作:

df = df.applymap(self._row_cleaner)['ComplainColumns']
但是
unicodeincoder错误一直是最大的问题。第一个在英镑符号上出现这个错误的地方

UnicodeEncodeError:“ascii”编解码器无法对646位置的字符u'\xa3'进行编码:序号不在范围(128)内。

尝试了以下内容:
df=df.apply(lambda x:x.unicode.replace(u'\xa3','')
gut不起作用


目标是将所有非字母字符替换为
'
'

如果要替换除[A-z0-9]以外的所有字符,则可以使用replace with regex,即

 df = df.replace('[^\w\s]','',regex=True)
数据框中可能缺少数据,因此您可能需要使用astype(str),因为您使用的是带有
的列表理解。lower()
,Nan将被视为浮点

df.astype(str).apply(cleanup)

这是否有助于
df=df.replace('[^\w\s]','',regex=True)
?不,它取代了一切,让我觉得它的工作有点小问题<代码>属性错误:(“'float'对象没有属性'lower',u'出现在索引正处”)
内部清理方法
removedStopWords=“”.join([str(i)表示行中的i.lower().split()(如果我不在停止中))
可能需要使用
df.astype(str)
然后
apply
数据框架中可能会出现
Nans
,我知道这只是为了确保你得到更多的信任,而不是作为评论:)