0

Im trying create a dataframe from array but I didnt have sucess.

    import investpy
    import pandas as pd

    result = ['BRKM5', 'MGLU3', 'PETR3', 'B3SA3', 'WEGE3']

    resultado =[]
    i=0
    for t in result:
    x = investpy.stocks.get_stock_information(t, 'brazil', as_json=False)    
    resultado.append(x.values)    
    i += 1

    df= pd.DataFrame(resultado, columns = ['Stock Symbol',
                            'Prev. Close',
                            'Todays Range',
                            'Revenue',
                            'Open',
                            '52 wk Range',
                            'EPS',
                            'Volume',
                            'Market Cap',
                            'Dividend (Yield)',
                            'Average Vol. (3m)',
                            'P/E Ratio',
                            'Beta',
                            '1-Year Change',
                            'Shares Outstanding',
                            'Next Earnings Date'])
df

ValueError: Shape of passed values is (5, 1), indices imply (5, 16)

1 Answer 1

1

First set the parameter as_json as true in the line. According to the docs setting it to True makes the function return a dict (which is what you were probably expecting)

x = investpy.stocks.get_stock_information(t, 'brazil', as_json=True)    

Then instead of using append use extend to add the values list of the dictionary values to the result list

resultado.extend(x.values()) 

Now everything should work.

Sign up to request clarification or add additional context in comments.

3 Comments

I tryed this @kinshukdua but with out sucess 'TypeError: 'numpy.ndarray' object is not callable'
I've updated the answer, now it should work.
I actually got it with set 'as_jason = True' . Thanks @kinshukdua

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.