理解 Pandas 中的 GroupBy 聚合计算
在处理大型数据集时,pandas 提供了一个名为 groupby() 的强大函数来对数据进行分组特定列并对分组数据执行计算。在这种情况下,让我们探讨如何利用 groupby() 计算组内值的总和。
考虑以下数据框,其中我们有有关个人在多个日期购买水果的详细信息:
| Fruit | Date | Name | Number | |---|---|---|---| | Apples | 10/6/2016 | Bob | 7 | | Apples | 10/6/2016 | Bob | 8 | | Apples | 10/6/2016 | Mike | 9 | | Apples | 10/7/2016 | Steve | 10 | | Apples | 10/7/2016 | Bob | 1 | | Oranges | 10/7/2016 | Bob | 2 | | Oranges | 10/6/2016 | Tom | 15 | | Oranges | 10/6/2016 | Mike | 57 | | Oranges | 10/6/2016 | Bob | 65 | | Oranges | 10/7/2016 | Tony | 1 | | Grapes | 10/7/2016 | Bob | 1 | | Grapes | 10/7/2016 | Tom | 87 | | Grapes | 10/7/2016 | Bob | 22 | | Grapes | 10/7/2016 | Bob | 12 | | Grapes | 10/7/2016 | Tony | 15 |
目标:计算按名称分组的水果购买总和
我们的目标是计算每个人购买的水果总数,按水果(Fruit)和人名(Name)对数据进行分组。
解决方案:使用 GroupBy.sum()
为了实现这一点,我们使用 groupby() 函数对列进行分组:
result = df.groupby(['Fruit', 'Name']).sum()
应用于分组数据的 sum() 方法会自动聚合指定列中的值(在本例中,Number 表示购买的水果数量)。
输出:
代码的输出为我们提供了聚合值:
| | Number | |----------------|--------| | Fruit | Name | | Apples | Bob | 16 | | | Mike | 9 | | | Steve | 10 | | Grapes | Bob | 35 | | | Tom | 87 | | | Tony | 15 | | Oranges | Bob | 67 | | | Mike | 57 | | | Tom | 15 | | | Tony | 1 |
在这里,我们可以观察每个人购买的水果总数在每个水果类别中。例如,在“鲍勃”组中,购买的“苹果”总数为 16 个,购买的“葡萄”总数为 35 个。
以上是Pandas 的'groupby()”函数如何计算组内值的总和?的详细内容。更多信息请关注PHP中文网其他相关文章!

ArraySareAryallyMoremory-Moremory-forigationDataDatueTotheIrfixed-SizenatureAntatureAntatureAndirectMemoryAccess.1)arraysStorelelementsInAcontiguxufulock,ReducingOveringOverheadHeadefromenterSormetormetAdata.2)列表,通常

ToconvertaPythonlisttoanarray,usethearraymodule:1)Importthearraymodule,2)Createalist,3)Usearray(typecode,list)toconvertit,specifyingthetypecodelike'i'forintegers.Thisconversionoptimizesmemoryusageforhomogeneousdata,enhancingperformanceinnumericalcomp

Python列表可以存储不同类型的数据。示例列表包含整数、字符串、浮点数、布尔值、嵌套列表和字典。列表的灵活性在数据处理和原型设计中很有价值,但需谨慎使用以确保代码的可读性和可维护性。

Pythondoesnothavebuilt-inarrays;usethearraymoduleformemory-efficienthomogeneousdatastorage,whilelistsareversatileformixeddatatypes.Arraysareefficientforlargedatasetsofthesametype,whereaslistsofferflexibilityandareeasiertouseformixedorsmallerdatasets.

theSostCommonlyusedModuleForCreatingArraysInpyThonisnumpy.1)NumpyProvidEseffitedToolsForarrayOperations,Idealfornumericaldata.2)arraysCanbeCreatedDusingsnp.Array()for1dand2Structures.3)

toAppendElementStoApythonList,usetheappend()方法forsingleements,Extend()formultiplelements,andinsert()forspecificpositions.1)useeAppend()foraddingoneOnelementAttheend.2)useextendTheEnd.2)useextendexendExendEnd(

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

金融、科研、医疗和AI等领域中,高效存储和处理数值数据至关重要。 1)在金融中,使用内存映射文件和NumPy库可显着提升数据处理速度。 2)科研领域,HDF5文件优化数据存储和检索。 3)医疗中,数据库优化技术如索引和分区提高数据查询性能。 4)AI中,数据分片和分布式训练加速模型训练。通过选择适当的工具和技术,并权衡存储与处理速度之间的trade-off,可以显着提升系统性能和可扩展性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版
中文版,非常好用

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),