Home > Blockchain >  Pandas: merging multiple rows into one column on one index
Pandas: merging multiple rows into one column on one index

Time:03-04

I have a dictionary of values:

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],
    }

I want to create a table in pandas where the keys of the dictionary are the index, with each index having multiple rows containing the dictionaries values, like so:

enter image description here

What method or tool would allow me to make a table like this?

CodePudding user response:

Use the Series constructor and explode:

df = pd.Series(d).explode().reset_index(name='price')

output:

    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
  • Related