简体   繁体   English

来自excel文件的python字典列表

[英]list of dictionaries in python from excel file

Hey guys so I'm making a dictionary file that has a table name and column name and table name is repeated many times for how many column names there are in the excel file.大家好,我正在制作一个字典文件,其中有一个表名和列名,表名重复多次以表示 excel 文件中有多少列名。 So for example例如

 | table_name| column_name|
 | ----------|------------|
 | players   |  name      |
 | players   |  height    |
 | players   |  age       |
 | teams     | name       |
 | teams     | city       |
 | teams     | owner      |

and it goes down and down.它越来越低。 I have around 1000 rows here, so I could type them up in the format that I desire but feels like it would take a lot of time.我这里有大约 1000 行,所以我可以按照我想要的格式输入它们,但感觉这会花费很多时间。 Here is the format that I'm trying to get in a list of dictionaries.这是我试图在字典列表中获取的格式。

[{'players':['name', 'height', 'age']}, {'teams':['name', 'city', 'owner']}, ....]

One option can be to read an excel file with pandas.一种选择是使用 pandas 读取 excel 文件。

You can use pandas.DataFrame.groupby() then get the result of groupby as list with apply .您可以使用pandas.DataFrame.groupby()然后使用apply将 groupby 的结果作为list At then end use pandas.Series.to_dict() .最后使用pandas.Series.to_dict()

import pandas as pd
file_path = "Book1.xlsx"
df = pd.read_excel(file_path)
# >>> df
#   table_name column_name
# 0    players        name
# 1    players      height
# 2    players         age
# 3      teams        name
# 4      teams        city
# 5      teams       owner

dct = df.groupby('table_name')['column_name'].apply(list).to_dict()

# dct -> {'players': ['name', 'height', 'age'], 'teams': ['name', 'city', 'owner']}

# For converting the above 'dict', you can use the below 'list comprehension':
lst_dct = [{k:v} for k,v in dct.items()]

print(lst_dct)

Output:输出:

[{'players': ['name', 'height', 'age']}, {'teams': ['name', 'city', 'owner']}]

ahh thanks I'mahdi, I actually didn't see your answer, and my answer is actually pretty close to yours, just posting it just in case there is a need for a dictionary of all tables minus the list.啊谢谢我是马赫迪,我实际上没有看到你的答案,我的答案实际上非常接近你的,只是发布它以防万一需要所有表减去列表的字典。 but just saw you also included that as well:).但刚刚看到你也包括了它:)。 Glad we came to the same conclusion here that pandas is a nice library to use.很高兴我们在这里得出了相同的结论,即 pandas 是一个很好用的库。

import pandas

def excel_to_dict():
   csvFile = pandas.read_csv('qv_columns.csv')
   tables_grouped = csvFile.groupby('TABLE_NAME'). 
   ['COLUMN_NAME'].agg(list)
   tables_dict = tables_grouped.to_dict()
   print(tables_dict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM