如何创建Excel电子表格内容的字典? [英] How to create a dictionary of the contents of an Excel spreadsheet?

查看:668
本文介绍了如何创建Excel电子表格内容的字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据Excel电子表格文件的内容在Python中构建字典.以下是电子表格的结构示例(两列):

I'm attempting to build a dictionary in Python based off the contents of an Excel spreadsheet file. Here is an example of how the spreadsheet is structured (two columns):

Col 1     Col 2
Hello     World
Hello     Earth
Hello     Planet
Hello     Mars
Hello     Moon
Hi        Pluto
Hi        Neptune
Hi        Jupiter

如何在Python中创建字典以使数据看起来像这样:

How do I create a dictionary in Python to make the data look like this:

[{'Hello': 'World', 'Earth', 'Planet', 'Mars', 'Moon'}, {'Hi': 'Pluto', 'Neptune', 'Jupiter'}]

我正在尝试让每个键包含多个值.

I'm attempting to have each key contain multiple values.

.csv文件更改为"Excel电子表格文件"
在代码中将括号更改为{}.抱歉,那是个意外.

changed .csv file to "Excel spreadsheet file"
changed parenthesis to {} in code. sorry, that was an accident.

推荐答案

通过Pandas,您可以执行以下操作:

With Pandas you can do this with:

import pandas as pd
df = pd.read_excel('data1.xlsx')
dictionary = df.to_dict()

这篇关于如何创建Excel电子表格内容的字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆