在另一列的值的总和中添加一个新列

2024-04-19 14:26:05 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我使用熊猫,并试图在“总计”中添加一个新的列,它是当年所有车辆数量的总和。你知道吗

由此:

    type            year     number

Private cars        2005    401638
Motorcycles         2005    138588
Off peak cars       2005    12947
Motorcycles         2005    846

对这样的事情:

 type            year       number       Total

Private cars        2005    401638      554019
Motorcycles         2005    138588
Off peak cars       2005    12947
Motorcycles         2005    846

Tags: number数量typeprivatecars事情yeartotal
3条回答

这提供了一个类似的数据帧:

total = df['numer'].sum()
df['Total'] = np.ones_line(df['number'].values) * total

使用GroupBy+transformsum

df['Year_Total'] = df.groupby('year')['number'].transform('sum')

请注意,这将为您提供每行的年度总数。如果您希望“清空”某些行的总计,那么应该精确地指定其逻辑。你知道吗

使用^{},然后在必要时替换重复的值:

df['Total'] = df.groupby('year')['number'].transform('sum')
print (df)
            type  year  number  Total
0   Private cars  2005       1      3
1    Motorcycles  2005       2      3
2  Off peak cars  2006       5     20
3    Motorcycles  2006       7     20
4   Motorcycles1  2006       8     20

df.loc[df['year'].duplicated(), 'Total'] = np.nan
print (df)
            type  year  number  Total
0   Private cars  2005       1    3.0
1    Motorcycles  2005       2    NaN
2  Off peak cars  2006       5   20.0
3    Motorcycles  2006       7    NaN
4   Motorcycles1  2006       8    NaN

可以替换为空值,但不建议这样做,因为使用字符串获取混合值,某些函数应失败:

df.loc[df['year'].duplicated(), 'Total'] = ''
print (df)
            type  year  number Total
0   Private cars  2005       1     3
1    Motorcycles  2005       2      
2  Off peak cars  2006       5    20
3    Motorcycles  2006       7      
4   Motorcycles1  2006       8      

相关问题 更多 >