1

I have a data like this,

finalResult = [{'40': {'A': 3.1, 'B': 5.62, 'C': 5.99, 'D': 5.06, 'E': 5.09}}, 
{'50': {'A': 2.95, 'B': 5.21, 'C': 5.41, 'D': 4.64, 'E': 4.5}}, 
{'60': {'A': 2.35, 'B': 4.8, 'C': 4.83, 'D': 4.08, 'E': 3.62}},
{'70': {'A': 1.94, 'B': 4.6, 'C': 4.41, 'D': 3.65, 'E': 3.62}]

I want to export this data to excel with some format like below,

    40      50      60      70    
A   3.1     2.95    2.35    1.94    
B   5.62    5.21    4.8     4.6     
C   5.99    5.41    4.83    4.41    
D   5.06    4.64    4.08    3.65    
E   5.09    4.5     3.62    3.62    

How to achieve this?

Finally the data should export to excel like below,

enter image description here

1 Answer 1

1

Use list comprehension with DataFrame contructor and concat:

df = pd.concat([pd.DataFrame(x) for x in finalResult],axis=1)
print (df)
     40    50    60    70
A  3.10  2.95  2.35  1.94
B  5.62  5.21  4.80  4.60
C  5.99  5.41  4.83  4.41
D  5.06  4.64  4.08  3.65
E  5.09  4.50  3.62  3.62

And then to_excel:

df.to_excel(`file.xlsx`)
Sign up to request clarification or add additional context in comments.

1 Comment

Ok fine. But I need to export into excel !

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.