2016-12-07 63 views
0

我有一個數據幀:大熊貓everyline正則表達式

#!/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 

我想用正則表達式

df.c=re.search('((\d+)-){2}(\d+)',df.c).group() 

我知道這p21蛋白表達是錯誤: re模塊用於str,但df.c是pandas.series,但我不知道如何爲熊貓的每一行編寫正則表達式

回答

1

可以使用pandas內置的正則表達式匹配.str.extract()方法:

df['c'] = df.c.str.extract('(\d+-\d+-\d+)') 

enter image description here