如何在 pandas 数据框中组织多个股票数据进行绘图
How to organise multiple stock data in pandas dataframe for plotting
我有一百多只股票(实际上是加密货币,但这并不重要)我想绘制,都在同一条线上。
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] │
│ │ │ │
└──────────────┴──────────────────┴──────────────────────────────────────┘
我不知道如何从这个数据框中绘制线图,我什至不知道这是否可能。有办法吗?还是有更好的方法构建数据?
如果按照下面的示例所示转换数据可能会更好。
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
stock
price
time
A
10
1
A
20
2
A
30
3
A
40
4
B
1
1
B
2
2
B
3
3
B
4
4
然后,用plotly.express
用
画出每只股票的折线图
import plotly.express as px
px.line(df, color='stock', x='time', y='price')
输出:
我有一百多只股票(实际上是加密货币,但这并不重要)我想绘制,都在同一条线上。
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] │
│ │ │ │
└──────────────┴──────────────────┴──────────────────────────────────────┘
我不知道如何从这个数据框中绘制线图,我什至不知道这是否可能。有办法吗?还是有更好的方法构建数据?
如果按照下面的示例所示转换数据可能会更好。
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
stock | price | time |
---|---|---|
A | 10 | 1 |
A | 20 | 2 |
A | 30 | 3 |
A | 40 | 4 |
B | 1 | 1 |
B | 2 | 2 |
B | 3 | 3 |
B | 4 | 4 |
然后,用plotly.express
用
import plotly.express as px
px.line(df, color='stock', x='time', y='price')
输出: