import pandas as pd
df = pd.DataFrame(data={'data': ['2 (B) - 15 (K)']})
print(df)
Current DataFrame:
data
0 2 (B) - 15 (K)
What am looking to do is to extract 2, B, 15 and K into 4 new columns within the same dataframe.
is that possible using pandas.regex directly?
>Solution :
With the same pattern spaces-parenthesis-dash and empty strings, this way works
df = pd.DataFrame(data={'data': ['2 (B) - 15 (K)', '']})
print(df['data'].str.extract('(\d*).\((.)\).-.(\d*).\((.)\)'))
# 0 1 2 3
# 0 2 B 15 K
# 1 NaN NaN NaN NaN