我有个数据:
#!/usr/bin/python
# encoding=utf-8
df=pandas.DataFrame([[1,2,'2015-11入住,2015-11-12离开'],[2,3,'2016-11入住,2016-11-2离开']],columns=['a','b','c'])
print df我想得到结果:
a b c
0 1 2 2015-11-12
1 2 3 2016-11-2我想用regex
df.c=re.search('((\d+)-){2}(\d+)',df.c).group()我知道这个表达式是错误的:re模块用于str,但df.c是一个pandas.series,但我不知道如何为熊猫的每一行编写正则表达式
发布于 2016-12-07 03:15:51
您可以使用pandas内置正则表达式匹配.str.extract()方法:
df['c'] = df.c.str.extract('(\d+-\d+-\d+)')

https://stackoverflow.com/questions/41008761
复制相似问题