python - 如何从 Excel 中访问值,其中第一列是标题

标签 python excel pandas scripting xlrd

我刚开始使用 python 访问 Excel 中的数据。 我需要这样的东西。

[Id:{1,2,3}, Name:{Raj,Rahul,Dave}, Age:{28,29,29}, City:{Delhi,Mumbai,Bengaluru}]

我需要Python字典列表中的这种数据。

这是Excel数据:

enter image description here

最佳答案

这是使用 pandas 库实现此操作的方法:

import pandas as pd

df= pd.read_excel('your_file.xlsx')

df = df.T
df.columns = df.iloc[0]
df = df.drop(df.index[0])

df.to_dict()

输出:

{'Name': {1: 'Raj', 2: 'Rahul', 3: 'Dave'},
 'Age': {1: 28, 2: 29, 3: 29},
 'City': {1: 'Delhi', 2: 'Mumbai', 3: 'Bengaluru'}}

关于python - 如何从 Excel 中访问值,其中第一列是标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57671760/

相关文章:

python - 从现有的 DataFrame 构建分层索引的 DataFrame

Python Requests 拆分 TCP 数据包

python - 在 Django 的 ModelChoiceField 中访问对象

java - 读取一个excel表格并将其插入数据库mysql

swift - XLSX(Excel)底层XML结构

python - Pandas:组合具有不同时间频率的列

python - 如何从Python脚本中包装交互式ssh session 的调用?

python - 有没有办法找到直方图局部最大值的范围?

arrays - 可变大小的公共(public)数组变量

python - map pandas 内的 Lambda 函数