简体   繁体   English

包含CSV文件的词典列表中的元素的平均值

[英]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. 我必须创建一个带有两个参数(.csv文件的名称)的函数,其中它计算某个人的宠物的年龄的平均值。

I have two CSV files. 我有两个CSV文件。 The first one contains the info of the pets. 第一个包含宠物的信息。 Like this: 像这样: 在此输入图像描述

The second one contains the name of the owner's and which pets they own.Like this: 第二个包含所有者的名字以及他们拥有的宠物。就像这样: 在此输入图像描述

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. 我的函数需要读取此.csv文件并返回另一个.csv文件,其中包含宠物年龄的平均值,由其所有者区分。 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. 例如,约翰有三只宠物(Michalengelo,Leonardo和Raphael),因此这些函数读取两个.csv文件并计算John的宠物年龄的平均值。 The same goes for Adam and Eva. 亚当和伊娃也是如此。

I have a function that takes the csv file and turn it into a dict. 我有一个函数,它接受csv文件并将其转换为dict。 For example (with the first csv file): 例如(使用第一个csv文件):

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. 提前致谢。

The easiest way is to use pandas module which you can learn in 10 minutes. 最简单的方法是使用pandas模块,您可以在10分钟内学习。

Consider your data is like this in separate csv files: 在单独的csv文件中考虑您的数据是这样的:

在此输入图像描述 在此输入图像描述

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
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 Python代码

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

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

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