您可以创建一个映射如果你想字符从字符串中删除,并str.translate
他们:
In [29]: tbl = dict.fromkeys(range(128), u"")
In [30]: s = u'Ümeå'
In [31]: print(s.translate(tbl))
Üå
在大熊猫w ^你似乎正在使用你可以使用pandas.Series.str.translate。
Series.str.translate(表,deletechars =无)
Map all characters in the string through the given mapping table. Equivalent to standard str.translate(). Note that the optional argument deletechars is only valid if you are using python 2. For python 3, character deletion should be specified via the table argument.
translate
将是更有效的比str.join
:
In [7]: s = 'Ümeå' * 1000
In [8]: timeit ''.join([x for x in s if ord(x) > 127])
1000 loops, best of 3: 489 µs per loop
In [9]: timeit s.translate(tbl)
1000 loops, best of 3: 289 µs per loop
In [10]: s.translate(tbl) == ''.join([x for x in s if ord(x) > 127])
Out[10]: True
对于python2大熊猫,你需要deletechars
无:
In [2]: import pandas as pd
In [3]: raw_data = {'Name' : pd.Series(['david','åndrëw','calvin'], index=['a', 'b', 'c'])}
In [4]: df = pd.DataFrame(raw_data, columns = ['Name'])
In [5]: delete = "".join(map(chr,range(128)))
In [6]: print df['Name'].str.translate(None, delete)
a
b åë
c
Name: Name, dtype: object
使用dict python3正常工作:
In [9]: import pandas as pd
In [10]: raw_data = {'Name' : pd.Series(['david','åndrëw','calvin'], index=['a', 'b', 'c'])}
In [11]:
In [11]: df = pd.DataFrame(raw_data, columns = ['Name'])
In [12]:
In [12]: delete = dict.fromkeys(range(128), "")
In [13]: df['Name'].str.translate(delete)
Out[13]:
a
b åë
c
Name: Name, dtype: object
需要都记录不同的方法:
参数:
table : dict (python 3), str or None (python 2) In python 3, table is a mapping of Unicode ordinals to Unicode ordinals, strings, or None. Unmapped characters are left untouched. Characters mapped to None are deleted. str.maketrans() is a helper function for making translation tables. In python 2, table is either a string of length 256 or None. If the table argument is None, no translation is applied and the operation simply removes the characters in deletechars. string.maketrans() is a helper function for making translation tables. deletechars : str, optional (python 2) A string of characters to delete. This argument is only valid in python 2.
请降低代码的短,**完整* *显示问题的程序。将该程序完整复制粘贴到您的问题中。有关更多信息,请参见[问]和[mcve]。 –
这是Python3吗? –
使用['codec.decode(string,errors ='ignore')'](https://docs.python.org/2/library/codecs.html#codec-base-classes) –