Home > Back-end >  How do I extract data from a DataFrame using regular expressions?
How do I extract data from a DataFrame using regular expressions?

Time:11-17

I am trying to correct data in a DataFrame and am facing a value replacement problem. The original value comes in the format "31 ^" or "54_", I need it to come in the format Integer for example 31.54

frame = pd.DataFrame({'first': [123, '32^'], 'second': [23,'13_']})
frame['first'] = frame['first'].str.extract(r'([0-9] )', expand=False)


first   second
0   NaN 23
1   32  13_

CodePudding user response:

Use Series.str.extract with fillna:

In [679]: frame['first'] = frame['first'].str.extract('(\d )').fillna(frame['first'])

In [680]: frame['second'] = frame['second'].str.extract('(\d )').fillna(frame['second'])

In [681]: frame
Out[681]: 
  first second
0   123     23
1    32     13
  • Related