将字典转换为数据帧(以重复的键作为行)

Arn*_*iva 2 python dictionary for-loop dataframe pandas

我正在尝试将以下字典:partlistzonesdef(有50个键)转换为数据框:可以说我们有字典:

{1: [60, 127],
 2: [21, 43, 61, 19],
 3: [186, 154, 37],
 4: [99, 68, 80, 87, 128, 98]}
Run Code Online (Sandbox Code Playgroud)

我怎样才能将其转换为这样的数据框:

    Index Area
0   1     60
1   1     127
2   2     21
3   2     43
4   2     61
5   2     19
6   3     186
7   3     154
8   3     37
Run Code Online (Sandbox Code Playgroud)

等等?

Cor*_*ien 7

创建一个Series并将列表的每个元素转换为一行,explode然后reset_index得到预期的结果:

df = pd.Series(d, name='Area').rename_axis('Index').explode().reset_index()
Run Code Online (Sandbox Code Playgroud)

输出:

>>> df
    Index Area
0       1   60
1       1  127
2       2   21
3       2   43
4       2   61
5       2   19
6       3  186
7       3  154
8       3   37
9       4   99
10      4   68
11      4   80
12      4   87
13      4  128
14      4   98
Run Code Online (Sandbox Code Playgroud)