Home > Mobile >  Extract from pattern if present, else keep as-is
Extract from pattern if present, else keep as-is

Time:04-21

Say I have the following series:

ser = pd.Series(['abc', '(d:affds)', 'trrt'])

If an element starts with (d:) and ends with ), I'd like to extract what's inside. Else, I'd like to keep it as-is.

If I do

ser.str.extract(r'\(d:(.*)\)')

then I get

       0
0    NaN
1  affds
2    NaN

So, the extraction worked fine for row 1. However, for rows 0 and 2, I'd like to keep the row as-is. So, my expected output is:

       0
0    abc
1  affds
2   trrt

CodePudding user response:

You may add fillna

ser.str.extract(r'\(d:(.*)\)')[0].fillna(ser)
Out[163]: 
0      abc
1    affds
2     trrt
Name: 0, dtype: object
  • Related