简体   繁体   English

如何在python中遍历数据框中的一列,得到该行对应的算术计算

[英]How to iterate through a column in data frame in python and get the arithmetic calculation corresponding to the row

Data Frame:(small data frame provided) (actual data frame is large)数据框:(提供小数据框)(实际数据框很大)

Item Type物品种类 Item Weight产品重量 grp cnt grp cnt
Baking Goods烘焙食品 4.880 4.880 5 5
Baking Goods烘焙食品 4.920 4.920 5 5
Baking Goods烘焙食品 5.260 5.260 5 5
Baking Goods烘焙食品 5.425 5.425 3 3
Breads面包 5.035 5.035 4 4
Breads面包 5.260 5.260 2 2
Breakfast早餐 7.895 7.895 9 9
Breakfast早餐 8.060 8.060 7 7

using above data frame calculations to be done:使用上述数据框计算来完成:

  1. Baking Goods = [(4.880 * 5) + (4.920 * 5) + (5.260 * 5) + (5.425 * 3)]烘焙食品 = [(4.880 * 5) + (4.920 * 5) + (5.260 * 5) + (5.425 * 3)]

    = (24.4 + 24.6 + 26.3 + 16.275)/(18) = (24.4 + 24.6 + 26.3 + 16.275)/(18)

  2. Breads =[(5.035 * 2)+(5.260 * 2)]面包=[(5.035 * 2)+(5.260 * 2)]

[same calculation as 1) according to data frame] [根据数据框计算与1)相同]

  1. Breakfast = [(7.895 * 9)+(8.060 * 7)]早餐 = [(7.895 * 9)+(8.060 * 7)]

[same calculation as 1) according to data frame] [根据数据框计算与1)相同]

I think you need multiple values and then aggregate sum like:我认为您需要多个值,然后汇总总和,例如:

df = df.assign(new = df['Item Weight'].mul(df['grp cnt'])).groupby('Item Type')['new'].sum()

try using groupby and then apply .尝试使用groupby然后apply

df = df.groupby('Item Type').apply(lambda x: x.prod(axis=1).sum()/x['grp cnt'].sum())

OR或者

x = df.set_index('Item Type')
df = x.prod(axis=1).groupby('Item Type').sum().div(x.groupby('Item Type')['grp cnt'].sum())

df:东风:

Item Type
Baking Goods    5.087500
Breads          5.110000
Breakfast       7.967187
dtype: float64

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

相关问题 如何按列和行遍历数据表? - How to iterate through a data table by column and row? Python:如何在数据框中传递3列作为函数中的3个独立参数并迭代列值 - Python: how to pass 3 columns in data frame as 3 separate arguments in function and iterate through the column values 如何遍历数据框单列中的行? - how to iterate through rows within single column of data frame? 遍历列表并添加以将 1 或 0 添加到数据框中的相应列 - iterate through a list and add to add an 1 or 0 to corresponding columns in a data frame 如何遍历 pyspark 中未知数据帧的列的行 - How to iterate through rows of a column of a unknown data-frame in pyspark 如何迭代此数据框 - 第一行没有第 1 列 - How do I iterate this Data frame - First row has no column 1 如何在python中使用数据框进行行级计算? - How to do calculation on row level with data frame in python? 如何遍历 Python 数据框,偶尔需要就地用一行替换两行? - How do I iterate through a Python data frame where I occasionally need to replace two rows with one row, in-place? 如何将每个列名和 append 迭代到 python 中的表或数据框 - How to iterate for each column name and append it to a table or data frame in python 如何使用一个工作簿中的列,遍历另一个工作簿并复制相应的数据 - How to use a column from one workbook, iterate through another and copy the corresponding data over
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM