如何通过在项目中添加标题将Panda数据框转换为Dict



https://i.stack.imgur.com/TEFp0.png

大家好,我有一个数据帧,如图片所示。我将在Django Rest Framework上使用它。所以我需要更改格式并做一些小的更改。。。

基本上,我想把它转换成dict,但我需要下面项目中的时间戳。。。如果有人能帮助我,我将不胜感激。。。

{
{
'datetime': '22-09-30'
'TotalRevenue': 'xxxx'
'CostOfRevenue': 'xxxx'
},
{
'datetime': '22-06-30'
'TotalRevenue': 'xxxx'
'CostOfRevenue': 'xxxx'
},
{
'datetime': '22-03-30'
'TotalRevenue': 'xxxx'
'CostOfRevenue': 'xxxx'
},
{
'datetime': '21-12-30'
'TotalRevenue': 'xxxx'
'CostOfRevenue': 'xxxx'
}
}

我对熊猫不太熟悉,所以我尝试了一些东西。。。

stockTicker = yf.Ticker(symbol + '.IS')
stockIncomeQuarterlyPanda = stockTicker.quarterly_income_stmt
stockIncomeQuarterlyChangeNan = stockIncomeQuarterlyPanda.fillna(0)
stockIncomeQuarterlyPandaToDict = stockIncomeQuarterlyChangeNan.to_dict()
stockIncomeQuarterly = stringify_keys(stockIncomeQuarterlyPandaToDict)

这是我的代码。。。

您显示的输出不是有效的dict。如果您想要一个dicts列表

[{"date": col, **df[col].to_dict()} for col in df.columns]

最新更新