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