我有一個值字典:
d = {
0: [1.61122, 1.61544, 1.60593, 1.60862, 1.61386],
3: [1.61962, 1.61734, 1.6195],
1: [1.5967, 1.59462, 1.59579],
2: [1.59062, 1.59279],
}
我想在 pandas 中創建一個表,其中字典的鍵是索引,每個索引都有多行包含字典值,如下所示:

什么方法或工具可以讓我制作這樣的表格?
uj5u.com熱心網友回復:
使用 Series 建構式和explode:
df = pd.Series(d).explode().reset_index(name='price')
輸出:
index price
0 0 1.61122
1 0 1.61544
2 0 1.60593
3 0 1.60862
4 0 1.61386
5 3 1.61962
6 3 1.61734
7 3 1.6195
8 1 1.5967
9 1 1.59462
10 1 1.59579
11 2 1.59062
12 2 1.59279
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/436683.html
上一篇:使用索引串列作為資料字典中的值
