Ricardo Proença
Ricardo Proença

Reputation: 13

how creat a Dataframe from an array

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)

Upvotes: 0

Views: 58

Answers (1)

kinshukdua
kinshukdua

Reputation: 2004

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.

Upvotes: 1

Related Questions