将字典的字典变成合理的 df

Getting dictionary id dictionarys into resonable df

无法将这本词典词典变成合理的 df。 希望它将所有代码显示为列,将属性显示为索引。

          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

你可以试试:

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

输出:

           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]

工作原理:

考虑一个示例数据框:

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]})

输入:

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

现在将 pd.Series 应用到 col_1

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

输出:

   val_1  val_2
0      1      3
1      4      5