Home > Back-end >  extract substring between characters
extract substring between characters

Time:07-09

string: {'Price_Min_COR': 21561.32, 'Price_Max_COR': 21600}

first character: 'Price_Min_COR':

two character: ,

code:df[df['LEVEL'].isna()==0].LEVEL.str.extract('("Price_Min_COR":.*?)\,')

result: Nan

need result : 21561.32

how to write regular expression ?

Open_Time_s=['2022-04-30 11:05:00 03:00','2022-04-30 11:10:00 03:00', np.nan, np.nan]
dici=[np.nan,np.nan,{'Price_Min_COR': 21561.32, 'Price_Max_COR': 21600},{'Price_Min_COR': 21561.32, 'Price_Max_COR': 21600}]
df = pd.DataFrame.from_dict({'OPEN_TIME':Open_Time_s,'dicts':dici})

print(df[df['dicts'].isna()==0].dicts.str.extract(r'Price_Min_COR:(.*)\b,'))

CodePudding user response:

Try this:

df['col1'].str.extract("'Price_Min_COR':\s(.*),")

Update

I think you have dictionary instead of a string,

Try this:

df['dicts'].str['Price_Min_COR']

Output:

0         NaN
1         NaN
2    21561.32
3    21561.32
Name: dicts, dtype: float64

CodePudding user response:

this should get you the result. use

Price_Min_COR.:(.*)\b,
extract(r'Price_Min_COR.:(.*)\b,')

here you can see the result https://regex101.com/r/HuTuaJ/1

  • Related