熊猫数据框

时间:2019-10-01 15:31:25

标签: pandas dataframe dictionary

import numpy as np
import pandas as pd
from pandas_datareader import data as wb
from yahoofinancials import YahooFinancials
sympol = [input()]
abc = YahooFinancials(sympol)
l=abc.get_financial_stmts('annual', 'cash')
df=pd.concat([pd.DataFrame(key) for key in l['cashflowStatementHistory']['FB']],axis=1,sort=True).reset_index().rename(columns={'index':'Time'})

谢谢您,但仅适用于特定的sympol。如何使其自定义,以便我可以输入任何符号。写什么而不是“ FB”。我尝试了[[sympol]],但它给了我一个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-164-6d2ff2d61183> in <module>
      6 abc = YahooFinancials(sympol)
      7 l=abc.get_financial_stmts('annual', 'cash')
----> 8 df=pd.concat([pd.DataFrame(key) for key in l['cashflowStatementHistory'][[sympol]]],axis=1,sort=True).reset_index().rename(columns={'index':'Time'})

TypeError: unhashable type: 'list'

您认为问题出在哪里?如何解决。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我认为最简单的是使用transposeDataFrame.TDataFrameconcat的使用列表理解。

如果以后需要使用TimeSeries,最好也创建DatetimeIndex

df = pd.concat([pd.DataFrame(x).T for x in l['cashflowStatementHistory']['FB']], sort=True)
df.index = pd.to_datetime(df.index)