如何在PANDAS DataFrame中组织多个库存数据以绘制
我有一百多个股票(实际上是加密货币,但这没关系)我想绘制所有这些,全部在同一条线上。
PriceTimeList = []
# Then I populate the PriceTimeList with dictionaries, one for each stock
getData()
# I iterate through i, for example, i = "BTC-PERP", i = "APPL-PERP"
# Under 'price' key, I have priceList which is a list of closing prices
# And I have it similarly or 'time' key
PriceTimeList.append({
'name': i,
'price': priceList,
'time': timeList
})
# I create a dataframe from the list of dictionaries
PriceTimeDF = pd.DataFrame(PriceTimeList)
# I change the index to use the 'name' column of my dataframe
PriceTimeDF = PriceTimeDF.set_index('name')
我最终得到了一个看起来像这样的数据框架:
┌──────────────┬──────────────────┬──────────────────────────────────────┐
│ │ │ │
│ │ price │ time │
├──────────────┼──────────────────┼──────────────────────────────────────┤
│ │ │ │
│ BTC-PERP │ [1,2,3,4,5] │ [1654052651, 1654052690, 1654052699] │
│ │ │ │
│ APPL-PERP │ [1,2,3,4,5] │ [1654052651, 1654052690, 1654052699] │
│ │ │ │
│ ETH-PERP │ [1,2,3,4,5] │ [1654052651, 1654052690, 1654052699] │
│ │ │ │
│ TSLA-PERP │ [1,2,3,4,5] │ [1654052651, 1654052690, 1654052699] │
│ │ │ │
└──────────────┴──────────────────┴──────────────────────────────────────┘
我不知道如何从此数据框架中制作线路,我什至不知道是否可能。有办法吗?还是我应该构建数据的更好方法?
I have over a hundred stocks (actually crypto but that does not matter) I wish to plot, all on the same line plot.
PriceTimeList = []
# Then I populate the PriceTimeList with dictionaries, one for each stock
getData()
# I iterate through i, for example, i = "BTC-PERP", i = "APPL-PERP"
# Under 'price' key, I have priceList which is a list of closing prices
# And I have it similarly or 'time' key
PriceTimeList.append({
'name': i,
'price': priceList,
'time': timeList
})
# I create a dataframe from the list of dictionaries
PriceTimeDF = pd.DataFrame(PriceTimeList)
# I change the index to use the 'name' column of my dataframe
PriceTimeDF = PriceTimeDF.set_index('name')
I end up with a dataframe that looks like this:
┌──────────────┬──────────────────┬──────────────────────────────────────┐
│ │ │ │
│ │ price │ time │
├──────────────┼──────────────────┼──────────────────────────────────────┤
│ │ │ │
│ BTC-PERP │ [1,2,3,4,5] │ [1654052651, 1654052690, 1654052699] │
│ │ │ │
│ APPL-PERP │ [1,2,3,4,5] │ [1654052651, 1654052690, 1654052699] │
│ │ │ │
│ ETH-PERP │ [1,2,3,4,5] │ [1654052651, 1654052690, 1654052699] │
│ │ │ │
│ TSLA-PERP │ [1,2,3,4,5] │ [1654052651, 1654052690, 1654052699] │
│ │ │ │
└──────────────┴──────────────────┴──────────────────────────────────────┘
I don't know how to make a line plot from this dataframe, I don't even know if it is possible. Is there a way? Or is there a better way I should structure the data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您喜欢示例Belo中所示的数据,也许更好。
然后使用
plotly.xplatly.xplats
通过使用使用的使用输出:
It maybe better if you transform the data as shown in example belo.
Then, use
plotly.express
to plot the line chart of each stocks by usingOutput: