How to use regex in a pandas dataframe.I want to extract numbers till an alphabet or string or a ( is encountered [closed]

Data looks like this now :’714M(7)’, ‘1196(-45)’, ‘426(-59)’, ‘4190(-314)’, ‘814(-33), Would just need the first valid number : 714, 1196 and so on.

Answer

Try:

df = pd.DataFrame({'col1': ['714M(7)',  '1196(-45)',  '426(-59)',  '4190(-314)',  '814(-33)',  '2212 (-87)',  '988 (-65)',  '61 (-9)',  '442 M(-4)', '161 (-18)',  '83 (-12)',  '263 (-117)']})

# print(df, 'nn')

df['col1'] = df['col1'].str.extract(r'(d+)[^d]')

print(df)

    col1
0    714
1   1196
2    426
3   4190
4    814
5   2212
6    988
7     61
8    442
9    161
10    83
11   263