在数据框的整个列中应用正则表达式 [英] Applying Regex across entire column of a Dataframe
本文介绍了在数据框的整个列中应用正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含 3 列的数据框:
I have a Dataframe with 3 columns:
id,name,team
101,kevin, marketing
102,scott,admin\n
103,peter,finance\n
我正在尝试应用正则表达式函数,以便删除不必要的空格.我有删除这些空格的代码,但我无法在整个 Dataframe 中循环它.
I am trying to apply a regex function such that I remove the unnecessary spaces. I have got the code that removes these spaces how ever I am unable loop it through the entire Dataframe.
这是我迄今为止尝试过的:
This is what I have tried thus far:
df['team'] = re.sub(r'[\n\r]*','',df['team'])
但这会引发错误 AttributeError: 'Series' object has no attribute 're'
谁能建议我如何在整个 Dataframe df['team']
列中循环这个正则表达式
Could anyone advice how could I loop this regex through the entire Dataframe df['team']
column
推荐答案
大功告成,有两种简单的方法可以做到:
You are almost there, there are two simple ways of doing this:
# option 1 - faster way
df['team'] = [re.sub(r'[\n\r]*','', str(x)) for x in df['team']]
# option 2
df['team'] = df['team'].apply(lambda x: re.sub(r'[\n\r]*','', str(x)))
这篇关于在数据框的整个列中应用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文