Python Pandas 提取 Excel 数据并转换为字典
可以使用 pandas 库中的 read_excel 函数来读取 excel 文件,并将其转换为字典。下面是一个示例代码:
import pandas as pd
# 读取 excel 文件
df = pd.read_excel('example.xlsx')
# 将 excel 内容转换为字典
data = df.to_dict()
# 打印字典内容
print(data)
在上面的示例中,首先使用 pd.read_excel 函数读取名为 example.xlsx 的 excel 文件,并将其存储在 DataFrame 对象 df 中。
然后,使用 to_dict 方法将 DataFrame 对象转换为字典,并将结果存储在 data 变量中。
最后,使用 print 函数打印字典内容。
请确保将 example.xlsx 替换为实际的 excel 文件路径。
原文地址: https://www.cveoy.top/t/topic/p38A 著作权归作者所有。请勿转载和采集!