Home > Back-end >  Extracting values from a special string type dataframe column
Extracting values from a special string type dataframe column

Time:01-23

I have a string-type pandas dataframe column:

{"min":[0,1,0.1,0,0,0], "max":[0,1,0.4,0,0,0]}  

df:

ID                min_max_config  
1    {"min":[0,1,0.1,0,0,0], "max":[0,1,0.4,0,0,0]}  
2    {"min":[0,1,0.1,0,0,0], "max":[0,1,0.5,0,0,0]}  
3    {"min":[0,1,0.6,0,0,0], "max":[0,1,0.7,0,0,0]}  
4    {"min":[0,1,0.8,0,0,0], "max":[0,1,0.2,0,0,0]}  

I want to make separate columns out of the sum of the values of min and max:

output_df:

ID.  min.   max                 min_max_config  
1.   1.1    1.4        {"min":[0,1,0.1,0,0,0], "max":[0,1,0.4,0,0,0]}  
2.   1.1    1.5        {"min":[0,1,0.1,0,0,0], "max":[0,1,0.5,0,0,0]}  
3.   1.6    1.7        {"min":[0,1,0.6,0,0,0], "max":[0,1,0.7,0,0,0]}  
4.   1.8    1.2        {"min":[0,1,0.8,0,0,0], "max":[0,1,0.2,0,0,0]}  

How to achieve this output

CodePudding user response:

Here is one way to do it with the help of ast.literal_eval and pandas.Series.str :

from ast import literal_eval
​
df["min_max_config"] = df["min_max_config"].apply(literal_eval)
​
out = df.assign(**{k: df['min_max_config'].str[k].apply(np.sum) for k in ["min", "max"]})

Output : ​

print(out)

   ID                                              min_max_config  min  max
0   1  {'min': [0, 1, 0.1, 0, 0, 0], 'max': [0, 1, 0.4, 0, 0, 0]}  1.1  1.4
1   2  {'min': [0, 1, 0.1, 0, 0, 0], 'max': [0, 1, 0.5, 0, 0, 0]}  1.1  1.5
2   3  {'min': [0, 1, 0.6, 0, 0, 0], 'max': [0, 1, 0.7, 0, 0, 0]}  1.6  1.7
3   4  {'min': [0, 1, 0.8, 0, 0, 0], 'max': [0, 1, 0.2, 0, 0, 0]}  1.8  1.2
  • Related