我有一个人员数据框,如下:
我正在尝试每 3 行向数据帧添加小计。最终结果应如下所示:
有没有办法有效地做到这一点?我尝试使用 groupby 获取 subtotal 数据框,然后迭代循环 Personnel 数据框并逐一添加每个小计行,但一定有更好的方法吗?
同样,如果我想按国家/地区获取小计,有没有办法有效地添加到原始数据帧?
例如
subtotal_df = personnel_df.groupby(personnel_df.index // 3).sum(numeric_only=False)
for idx, row in personnel_df.copy().iterrows():
pass
我确信可以使用散布的小计行创建您想要的数据框,但我对 pandas 的经验是您真的希望保持表格简单且一致。
您开始的表格每人一行,该人的每个属性一列。这是 pandas 期望的格式,我们可以使用所有很棒的 pandas 工具,如 groupby 等。
带有小计的表没有这个简单的解释,使用该表进行任何 pandas 操作都会很痛苦。
相反,我建议添加新列来跟踪小计
import pandas as pd
#create your input table
df = pd.DataFrame({
'Country':['UK','UK','US','US','FR','FR','FR','FR','SWE','SWE'],
'Name':['Tony','Alan','Lee','David','Steve','Paul','Micky','George','Nigel','John'],
'Salary':[50,45,60,35,65,48,62,80,64,42],
'Age':[18.00,16.77,65.53,76.17,77.80,72.82,78.07,46.72,83.83,96.22],
})
#create a new column which will be 0 for the first 3 rows, then 1 for the next 3, etc
df['subtotal_group'] = df.index//3
#use the new column to group and sum subtotals
df['salary_subtotal'] = df.groupby('subtotal_group')['Salary'].transform('sum')
df['age_subtotal'] = df.groupby('subtotal_group')['Age'].transform('sum')
#get subtotals by country in the same way
df['country_salary_total'] = df.groupby('Country')['Salary'].transform('sum')
print(df)
如果您不喜欢,可以随时删除
subtotal_group
栏!