pandas applying regex to replace values

You could use Series.str.replace:

import pandas as pd

df = pd.DataFrame(['$40,000*','$40000 conditions attached'], columns=['P'])
print(df)
#                             P
# 0                    $40,000*
# 1  $40000 conditions attached

df['P'] = df['P'].str.replace(r'\D+', '', regex=True).astype('int')
print(df)

yields

       P
0  40000
1  40000

since \D matches any character that is not a decimal digit.

Leave a Comment