当前位置:   article > 正文

Python:Pandas 读取excel数据并转换成list嵌套dict_pd 转 list[dict]

pd 转 list[dict]

Input:
在这里插入图片描述
Output:
[
{“A”: “a2”, “B”: “b2”, “C”: “c2”, “D”: “d2”},
{“A”: “a3”, “B”: “b3”, “C”: “c3”, “D”: “d3”},
{“A”: “a4”, “B”: “b4”, “C”: “c4”, “D”: “d4”}

]

code:

import pandas as pd

ex_data = pd.read_excel("test.xlsx") #默认读取第一个sheet的内容
head_list = list(ex_data.columns) #拿到表头: [A, B, C, D]
list_dic = []
for i in ex_data.values: # i 为每一行的value的列表:[a2, b2, c2, d2]
    a_line = dict(zip(head_list, i)) # a_line: {"A": "a2", "B": "b2", "C": "c2", "D": "d2"}
    list_dic.append(a_line)
print(list_dic)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/209732
推荐阅读
相关标签
  

闽ICP备14008679号