1

I have tried below code to convert my list object to dataframe, however, I keep getting error.

Please can someone advise?

Code:

  newframe = pd.DataFrame(np.array(basket).reshape(len(basket), -1),  
                 columns = ['currency', 'weights' , 'ric'])

ERROR :

ValueError: Shape of passed values is (1, 55), indices imply (3, 55)

List object:

<class 'list'>: [{'currency': 'EUR', 'weight': 806311901, 'ric': '_DIVISOR'},  
{'currency': 'EUR', 'weight': 89440570.0, 'ric': 'AFXG.DE'},  
{'currency': 'DKK', 'weight': 217059600.0, 'ric': 'AMBUb.CO'},  
{'currency': 'EUR', 'weight': 37692508.0, 'ric': 'ARGX.BR'},  
{'currency': 'GBp', 'weight': 1267101448.0, 'ric': 'AZN.L'},  
{'currency': 'EUR', 'weight': 932551965.0, 'ric': 'BAYGn.DE'}, 
{'currency': 'CHF', 'weight': 55400000.0, 'ric': 'BION.S'}]

Thanks for your help.

2
  • You are reshaping your array to be 1 dimensional (i.e. 1 column) whereas the dataframe specification is for 3 columns. That is why you get an error. If you post some actual data (i.e. more detail) we can have a go at correcting ti. Commented Mar 25, 2019 at 10:27
  • yes thanks for quick response - my actual data is posted in question Commented Mar 25, 2019 at 10:30

1 Answer 1

2
df = pd.DataFrame(list_of_dicts)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot - I was under impression to reshape my list first? will give this a go

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.