Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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_Dataframe - Fatal编程技术网

Python 将数据帧转换为字典

Python 将数据帧转换为字典,python,pandas,dataframe,Python,Pandas,Dataframe,我想将dataframe转换为键字典和值列表。 索引探针为键,框为值 product_fk vp_fk box_fk probe_fk '83348' 101 326 8548 '83348' 101 326 8549 '83348' 101 326

我想将dataframe转换为键字典和值列表。 索引探针为键,框为值

             product_fk   vp_fk         box_fk
probe_fk                                   
'83348'            101    326            8548
'83348'            101    326            8549
'83348'            101    326            8550
'83348'            101    326            8551
'105513'           107    357            8866
'105513'           107    357            8864
'111996'           107    365            9059
我希望最终结果如下:

{'83348':[8548, 8549, 8550,8551],
'105513':[8866, 8864],
'111996':[9059]}
试试这个:

In [95]: df.groupby(level=0)['box_fk'].apply(list).to_dict()
Out[95]: {'105513': [8866, 8864], '111996': [9059], '83348': [8548, 8549, 8550, 8551]}