如何在PANDAS DataFrame中组织多个库存数据以绘制

发布于 2025-02-03 11:42:46 字数 1851 浏览 3 评论 0原文

我有一百多个股票(实际上是加密货币,但这没关系)我想绘制所有这些,全部在同一条线上。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2025-02-10 11:42:46

如果您喜欢示例Belo中所示的数据,也许更好。

df = pd.DataFrame({
    'stock': ['A', 'B'],
    'price': [[10,20,30,40], [1,2,3,4]],
    'time': [[1,2,3,4], [1,2,3,4]]
})

df = df.set_index(['stock']).apply(pd.Series.explode).reset_index()
df
股票价格时间
a101
a202
a303
a404
b11
b22
b3 33
b44,

然后使用plotly.xplatly.xplats通过使用使用的使用

import plotly.express as px
px.line(df, color='stock', x='time', y='price')

输出:

在此处输入图像描述“

It maybe better if you transform the data as shown in example belo.

df = pd.DataFrame({
    'stock': ['A', 'B'],
    'price': [[10,20,30,40], [1,2,3,4]],
    'time': [[1,2,3,4], [1,2,3,4]]
})

df = df.set_index(['stock']).apply(pd.Series.explode).reset_index()
df
stockpricetime
A101
A202
A303
A404
B11
B22
B33
B44

Then, use plotly.express to plot the line chart of each stocks by using

import plotly.express as px
px.line(df, color='stock', x='time', y='price')

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文