Pandas DataFrame:从列中的字符串中删除不需要的部分 [英] Pandas DataFrame: remove unwanted parts from strings in a column
本文介绍了Pandas DataFrame:从列中的字符串中删除不需要的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种从DataFrame列中的字符串中删除不需要的部分的有效方法。
数据看起来像:
时间结果
1 09:00 + 52A
2 10:00 + 62B
3 11:00 + 44a
4 12:00 + 30b
5 13:00 -110a
我需要修剪这些数据到:
时间结果
1 09:00 52
2 10:00 62
3 11:00 44
4 12:00 30
5 13:00 110
我试过 .str.lstrip('+ - ')
和。 str.rstrip('aAbBcC')
,但是有一个错误:
TypeError:wrapper()只需要一个参数(2个给定)
任何指针都将不胜感激!
解决方案< div('x')'('x''')。rstrip('aAbBcC'))
I am looking for an efficient way to remove unwanted parts from strings in a DataFrame column.
Data looks like:
time result
1 09:00 +52A
2 10:00 +62B
3 11:00 +44a
4 12:00 +30b
5 13:00 -110a
I need to trim these data to:
time result
1 09:00 52
2 10:00 62
3 11:00 44
4 12:00 30
5 13:00 110
I tried .str.lstrip('+-')
and .str.rstrip('aAbBcC')
, but got an error:
TypeError: wrapper() takes exactly 1 argument (2 given)
Any pointers would be greatly appreciated!
解决方案
data['result'] = data['result'].map(lambda x: x.lstrip('+-').rstrip('aAbBcC'))
这篇关于Pandas DataFrame:从列中的字符串中删除不需要的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文