Stagg
Stagg

Reputation: 309

Average of element in list of dictionaries with CSV files

I have to make a function that takes two arguments(name of the .csv files), in which it calculates the average of the age of a certain person's pets.

I have two CSV files. The first one contains the info of the pets. Like this: enter image description here

The second one contains the name of the owner's and which pets they own.Like this:enter image description here

My functions needs to read this .csv files and return another .csv file with the average of the pets age, differenciated by their owner's. For example, John has three pets (Michalengelo, Leonardo and Raphael), so the functions reads the two .csv files and calculates de average of John's pets age. The same goes for Adam and Eva.

I have a function that takes the csv file and turn it into a dict. For example (with the first csv file):

read_csv_to_dict('Pets_info.csv'):
>>> [{'Age': '8', 'Name of the Pet': 'Felix', 'Species': 'Cat'}, {'Age': '57', 'Name of the Pet': 'Michelangelo', 'Species': 'Tortoise'}, {'Age': '12', 'Name of the Pet': 'Rantanplan', 'Species': 'Dog'}, {'Age': '2', 'Name of the Pet': 'Nemo', 'Species': 'Fish'}, {'Age': '45', 'Name of the Pet': 'Leonardo', 'Species': 'Tortoise'}, {'Age': '9', 'Name of the Pet': 'Milo', 'Species': 'Dog'}, {'Age': '57', 'Name of the Pet': 'Raphael', 'Species': 'Tortoise'}, {'Age': '4', 'Name of the Pet': 'Dory', 'Species': 'Fish'}]

I think if I manipulate these data with the dictionaries I can get what I want, I just don't know how to do it. Feel free to ask any questions if you don't undertand mine. Thanks in advance.

Upvotes: 1

Views: 235

Answers (2)

pmaniyan
pmaniyan

Reputation: 1046

pets.txt

Name of the Pet,Species,Age
Felix,Cat,8
Michelangelo,Tortoise,57
Rantarplan,Dog,12
Nemo,Fish,2
Leonardo,Tortoise,45
Milo,Dog,9
Raphael,Tortoise,57
Dory,Fish,4

owner.txt

Owner's Name,Pet Names
John,Michelangelo
Eva,Dory
Adam,Rantarplan
John,Leonardo
Eva,Felix
John,Raphael
Eva,Nemo

Python code

import pandas as pd
import numpy as np

l_pets = pd.read_csv('pets.txt')
l_owner = pd.read_csv('owner.txt')

l_merged = l_pets.merge(l_owner,how='inner',left_on='Name of the Pet',right_on='Pet Names')
l_groupded = l_merged.groupby(by="Owner's Name")

print l_groupded.aggregate(np.average)

Output

                    Age
Owner's Name
Adam          12.000000
Eva            4.666667
John          53.000000

Upvotes: 1

Abbas
Abbas

Reputation: 4069

The easiest way is to use pandas module which you can learn in 10 minutes.

Consider your data is like this in separate csv files:

enter image description here enter image description here

And this is what you can do in pandas:

import pandas as pd
#Read input csv files
own = pd.read_csv('OwenerPet.csv')
pet = pd.read_csv('PetAge.csv')
#Merge the dataframes on 'Pet Names'
ownpet = pd.merge(own,pet, on=['Pet Names'], how='inner')
#Group by owners and get the avarage
ownpetaverage = ownpet.groupby('Owners Name').mean()
#Print it, you could also save it by saying ownpetaverage.to_csv('average.csv')
print ownpetaverage

                   Age
Owners Name
Adam          7.000000
Eva          28.000000
John         22.666667

Upvotes: 2

Related Questions