[英]How can I strip off all non-numeric characters in a Pandas Series
我有一个 Pandas DataFrame。 我有兴趣获得只有数字字符的特定列。
例如,该列包含如下行:
4'> delay trip
4/
4'>book flight 'trip
34
4"> book flight delay
4"
如何去除所有非数字字符而只有这样的数字字符:
4
4
4
[3,4]
4
4
你在这里有两个不同的问题:
只需链接两个操作:
df[col].str.findall(r'\d').apply(lambda x: x[0] if len(x) == 1 else '' if len(x) == 0 else x)
用你的例子它给出:
0 4
1 4
2 4
3 [3, 4]
4 4
5 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.