从dict创建一个数据框,其中值是可变长度列表

spi*_*edd 5 python dataframe pandas

我有一个dict,其中值是一个列表,例如;

my_dict = {1: [964725688, 6928857],
           ...

           22: [1667906, 35207807, 685530997, 35207807],
           ...
           }
Run Code Online (Sandbox Code Playgroud)

在此示例中,列表中的最大项目为4,但可能大于该项目.

我想将其转换为数据帧,如:

1  964725688
1  6928857
...
22 1667906
22 35207807
22 685530997
22 35207807
Run Code Online (Sandbox Code Playgroud)

All*_*len 1

#Load dict directly to a Dataframe without loops
df=pd.DataFrame.from_dict(my_dict,orient='index')

#Unstack, drop na and sort if you need.
df.unstack().dropna().sort_index(level=1)
Out[382]: 
0  1     964725688.0
1  1       6928857.0
0  22      1667906.0
1  22     35207807.0
2  22    685530997.0
3  22     35207807.0
dtype: float64
Run Code Online (Sandbox Code Playgroud)