将字典转换为pandas中的DataFrame



我使用以下代码:

import pandas as pd
from yahoofinancials import YahooFinancials
mutual_funds = ['PRLAX', 'QASGX', 'HISFX']
yahoo_financials_mutualfunds = YahooFinancials(mutual_funds)
daily_mutualfund_prices = yahoo_financials_mutualfunds.get_historical_price_data('2015-01-01', '2021-01-30', 'daily')

我得到一个字典作为输出文件。我想获得一个pandas数据框,包含以下列:data, PRLAX, QASGX, HISFX,其中数据是formatted_date和每个股票的开盘价

熊猫dataframe

你可以这样做:

df = pd.DataFrame({
a: {x['formatted_date']: x['adjclose'] for x in daily_mutualfund_prices[a]['prices']} for a in mutual_funds
})

给了:

PRLAX      QASGX      HISFX
2015-01-02  19.694817  17.877445  11.852874
2015-01-05  19.203604  17.606575  11.665626
2015-01-06  19.444574  17.316357  11.450289
2015-01-07  19.963596  17.616247  11.525190
2015-01-08  20.260176  18.003208  11.665626
...               ...        ...        ...
2021-01-25  21.799999  33.700001  14.350000
2021-01-26  22.000000  33.139999  14.090000
2021-01-27  21.620001  32.000000  13.590000
2021-01-28  22.120001  32.360001  13.990000
2021-01-29  21.379999  31.709999  13.590000
[1530 rows x 3 columns]

或字典中的任何其他值

相关内容

  • 没有找到相关文章

最新更新