Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从中的字典值创建多个行和列_Python_Pandas - Fatal编程技术网

Python 如何从中的字典值创建多个行和列

Python 如何从中的字典值创建多个行和列,python,pandas,Python,Pandas,我有一个如下的数据框 id, values 1, {'foo':2 , 'bar':2} 2, {'baz':20} and so on 我想把这个数据帧转换成 id val1 val2 1 foo 2 1 bar 2 2 baz 20 等等。。 我不想重复数据帧中的每一行。。正如我非常确定的那样,pandas中有一种方法可以实现上述功能?IIUC PS:您可以在末尾添加重命名 df.set_index('id')['values'].apply(pd.Series

我有一个如下的数据框

id,  values
1, {'foo':2 , 'bar':2}
2, {'baz':20}
and so on
我想把这个数据帧转换成

id val1 val2
1  foo    2
1 bar     2
2 baz     20
等等。。
我不想重复数据帧中的每一行。。正如我非常确定的那样,pandas中有一种方法可以实现上述功能?

IIUC PS:您可以在末尾添加
重命名

df.set_index('id')['values'].apply(pd.Series).stack().reset_index()
Out[920]: 
   id level_1     0
0   1     bar   2.0
1   1     foo   2.0
2   2     baz  20.0