Home > Back-end >  Getting dictionary id dictionarys into resonable df
Getting dictionary id dictionarys into resonable df

Time:10-11

having trouble getting this dictionary of dictionaries into a resonable df. want it to display all the tickers as columns and the attributes as an index.

          Columns: 'AZN.ST','ERIC-B.ST','SAND.ST'
Index:
accountsPayable.    value 1. value 2.   value 3
capitalSurplus      value 1. value 2.   value 3
cash                value 1. value 2.   value 3
etc...
import pandas as pd

tickers = ['AZN.ST','ERIC-B.ST','SAND.ST']

yahoo_financials = YahooFinancials(tickers)

state = yahoo_financials.get_financial_stmts('quarterly','balance',reformat=True)
data = state['balanceSheetHistoryQuarterly']
newdf = pd.DataFrame.from_dict(data)
newdf

#getting the latest reports
finaldf = pd.DataFrame.from_dict(newdf.iloc[0])
finaldf

Code

enter image description here

CodePudding user response:

You can try:

finaldf = finaldf[0].apply(pd.Series)['2021-06-30'].apply(pd.Series)

Output:

           intangibleAssets  ...  deferredLongTermLiab
AZN.ST         2.000600e 10  ...                   NaN
ERIC-B.ST      4.272000e 09  ...          3.798000e 09
SAND.ST        2.113600e 10  ...                   NaN

[3 rows x 29 columns]

How it works:

Consider a sample dataframe:

col_1 = {'val_1':1, 'val_2':3}
col_3 = {'val_1':4, 'val_2':5}
df = pd.DataFrame(data={'col_1':[col_1, col_3]})

Input:

                      col_1
0  {'val_1': 1, 'val_2': 3}
1  {'val_1': 4, 'val_2': 5}

Now apply pd.Series to col_1

df = df['col_1'].apply(pd.Series)

Output:

   val_1  val_2
0      1      3
1      4      5
  • Related