Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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,我有一个这样的数据帧 3 1388 4 1388 5 IBM 8 157.75 9 88929 1021 1500 854 n 388 157.75 394 157.75 474 157.75 1584 889

我有一个这样的数据帧

3                1388
4                1388
5                 IBM
8              157.75
9               88929
1021             1500
854                 n
388            157.75
394            157.75
474            157.75
1584            88929
444          20160713
459    93000546718000
461                 7
55     93000552181000
22              89020
400            157.75
361              0.73
981                 0
16    1468416600.6006
18    1468416600.6006
362              0.46
我想将此数据帧转换为
{3:1388,4:1388,5:IBM,…}

我该怎么做

我有重复的数据帧,但它必须接受

df = pd.DataFrame({'col1': [1, 2],
                      'col2': [0.5, 0.75]},
                       index=['a', 'b'])



print(df)

   col1  col2
a     1   0.50
b     2   0.75

df.to_dict(){'col1': {'a': 1, 'b': 2}, 'col2': {'a': 0.5, 'b': 0.75}}
这是一个小示例,您可以尝试此方法

使用
df.to_dict()