生成表格
data = [[1,2,3],[4,5,6],[7,8,9]]
df = pd.DataFrame(data,columns=['a','b','c'])
df
方法一
{each:list(df[each]) for each in df.columns}
{‘a’: [1, 4, 7], ‘b’: [2, 5, 8], ‘c’: [3, 6, 9]}
方法二
[{k:v for k,v in df.loc[each].items()} for each in df.index]
[{‘a’: 1, ‘b’: 2, ‘c’: 3}, {‘a’: 4, ‘b’: 5, ‘c’: 6}, {‘a’: 7, ‘b’: 8, ‘c’: 9}]
方法三
df.to_dict(orient="list")
{‘a’: [1, 4, 7], ‘b’: [2, 5, 8], ‘c’: [3, 6, 9]}
方法四
df.to_dict(orient="dict")
{‘a’: {0: 1, 1: 4, 2: 7}, ‘b’: {0: 2, 1: 5, 2: 8}, ‘c’: {0: 3, 1: 6, 2: 9}}