1

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

2
  • Kindly share your dataframe as a dictionary, so it is reproducible: df.to_dict() Commented Jan 22, 2023 at 12:19
  • so the column min_max_config currently contains strings or dicts ? Commented Jan 22, 2023 at 12:22

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.