This is a continuation of Excel Aggregation with Python pandas Part 1. The place where the function that connects dict is called twice is awkward. I want to make it one function. One solution is to make it variable.
windows10 python3.9 VS Code Pandas openpyxl
Assuming that python and pandas have been installed (using pip)
Added function concatenateDicts that can have multiple arguments of dict type.
pandas_lib.py
import pandas as pd #pandas is a library that provides functions to support data analysis in Python.
import numpy as np
#Group by key and count
def countByKeyFromFileAndSheet(filename, sheetname, key):
df = pd.read_excel(filename, sheet_name=sheetname, engine="openpyxl")
dict = df[key].value_counts().to_dict()
return dict
def concatenateDicts(*dicts):
newdict = {} # empty dict
for dict in dicts:
print(dict)
for k in dict:
if k in newdict: # key detected in newdict
newdict[k] += dict[k]
else: # not detected
newdict[k] = dict[k]
return newdict
pandas_main.py I was able to call concatenateDicts once instead of calling concatenateDict twice.
pandas_main.py
import pandas_lib as pl
dict1 = pl.countByKeyFromFileAndSheet("./example.xlsx", "Sheet1", "Department")
dict2 = pl.countByKeyFromFileAndSheet("./example.xlsx", "Sheet2", "Department")
dict3 = pl.countByKeyFromFileAndSheet("./example.xlsx", "Sheet3", "Department")
dict = pl.concatenateDicts(dict1, dict2, dict3)
print(dict)
Of course, the result is the same.
{'Sales department': 4, 'Development department': 3, 'General Affairs Department': 3}
{'Sales department': 5, 'Development department': 3, 'Accounting department': 2}
{'Development department': 9, 'Accounting department': 1}
{'Sales department': 9, 'Development department': 15, 'General Affairs Department': 3, 'Accounting department': 3}
Recommended Posts