0

I want to create a dictionary from the values imported from the excel file using python, the excel columns file looks like this:

University Year
IUB 2013
IUB 2013
IUB 2013
IUB 2014
IUB 2015
BZU 2013
BZU 2013
BZU 2014
UCP 2016
UCP 2016
UCP 2013
UCP 2014

The output should look like this :

         'IUB': {'2013': '3', '2014': '1', '2015': '1'}, 
         'BZU': {'2013': '2', '2014': '1'}, 
         'UCP': {'2013': '1', '2014': '1', '2016': '2'}

1 Answer 1

1

You can use pandas to read your Excel file. Then use groupby ('University, 'Year') and agg to calculate the count for each University/Year.

Format your DataFrame with pivot then export to dictionary:

import pandas as pd
df = pd.read_excel("your_excel_file.xlsx")
df['count'] = 0
df = df.groupby(['University', 'Year'], as_index=False)['count'].agg('count')
df = df.pivot(index="Year", columns="University", values="count")
output = df.to_dict()
print(output)

Output:

{'BZU': {2013: 2.0, 2014: 1.0, 2015: nan, 2016: nan}, 'IUB': {2013: 3.0, 2014: 1.0, 2015: 1.0, 2016: nan}, 'UCP': {2013: 1.0, 2014: 1.0, 2015: nan, 2016: 2.0}}

You'll have to remove nan values manually if necessary:

for uni, year in output.items():
   for y, count in list(year.items()):
      if pd.isna(count):
         del year[y]

print(output)

Output:

{'BZU': {2013: 2.0, 2014: 1.0}, 'IUB': {2013: 3.0, 2014: 1.0, 2015: 1.0}, 'UCP': {2013: 1.0, 2014: 1.0, 2016: 2.0}}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your very informative answer. Can you please tell me what should I have to do If I have one additional column and I want to make It key also.
It depends on where you want your new key. Feel free to open a new question and ask exactly what you need with column content and position of the key!
{'BZU': {'IT' : {2013: 1.0, 2014: 1.0, 2015: nan, 2016: 2.0}}} {'UCP': {'CS': {2013: 1.0, 2014: 1.0, 2015: nan, 2016: 2.0}}}. Can you please help me in Making this type of list as I am not able to open a new question.
It's not straight-forward to create a nested dict from a DataFrame but the question has already been asked: stackoverflow.com/questions/19798112/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.