【问题标题】:Average of element in list of dictionaries with CSV files包含 CSV 文件的字典列表中元素的平均值
【发布时间】:2016-04-28 18:17:56
【问题描述】:

我必须创建一个接受两个参数(.csv 文件的名称)的函数,在该函数中计算某个人的宠物的平均年龄。

我有两个 CSV 文件。第一个包含宠物的信息。像这样:

第二个包含主人的名字和他们拥有的宠物。像这样:

我的函数需要读取这个 .csv 文件并返回另一个 .csv 文件,其中包含宠物的平均年龄,由主人的年龄区分。例如,John 有三只宠物(Michalengelo、Leonardo 和 Raphael),因此函数会读取两个 .csv 文件并计算 John 宠物年龄的平均值。亚当和伊娃也是如此。

我有一个函数可以获取 csv 文件并将其转换为字典。例如(使用第一个 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'}]

我想如果我用字典处理这些数据,我可以得到我想要的,我只是不知道该怎么做。 如果您不理解我的,请随时提出任何问题。 提前致谢。

【问题讨论】:

    标签: python python-2.7 csv dictionary


    【解决方案1】:

    最简单的方法是使用pandas 模块,你可以在 10 分钟内学会。

    考虑您的数据在单独的 csv 文件中是这样的:

    这就是你可以在 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
    

    【讨论】:

    • 我是 python 新手,不太了解 panda 模块的使用......
    • 非常感谢您的帮助!我正在使用 Idle,它说没有模块名称 pandas,我如何在我的计算机上安装它?
    【解决方案2】:
    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 代码

    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)
    

    输出

                        Age
    Owner's Name
    Adam          12.000000
    Eva            4.666667
    John          53.000000
    

    【讨论】:

    • 它给了我一个关键错误:'Pet Names'
    • 当输入文件和代码的列名错误时会发生这种情况。 owner.txt 应该是:\n Owner's Name,Pet Names John,Michelangelo Eva,Dory Adam,Rantarplan John,Leonardo Eva,Felix John,Raphael Eva,Nemo \n 并且 pets.txt 应该是:\n 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 \n 我用过列名仅根据您的屏幕截图。
    • 所有的列名都按这个顺序并且正确,我不知道为什么它给我一个错误
    • 请您粘贴您的代码以及输入文件
    • 我已编辑我的答案以提供输入文件、代码及其输出。
    猜你喜欢
    • 2022-01-15
    • 2021-12-03
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2021-09-30
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多