值错误:无法插入 ID,已存在 [英] ValueError: cannot insert ID, already exists

查看:171
本文介绍了值错误:无法插入 ID,已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据:

ID   TIME
1    2
1    4
1    2
2    3

我想按ID对数据进行分组,并计算每组的平均时间和大小.

I want to group the data by ID and calculate the mean time and the size of each group.

ID   MEAN_TIME COUNT
1    2.67      3
2    3.00      1

如果我运行此代码,则会收到错误ValueError:无法插入 ID,已存在":

If I run this code, then I get an error "ValueError: cannot insert ID, already exists":

result = df.groupby(['ID']).agg({'TIME': 'mean', 'ID': 'count'}).reset_index()

推荐答案

使用参数 drop=True 不使用 index 创建新列而是删除它:

Use parameter drop=True which not create new column with index but remove it:

result = df.groupby(['ID']).agg({'TIME': 'mean', 'ID': 'count'}).reset_index(drop=True)
print (result)
   ID      TIME
0   3  2.666667
1   1  3.000000

但是如果需要索引中的新列需要先rename旧列名:

But if need new column from index need rename old column names first:

result = df.groupby(['ID']).agg({'TIME': 'mean', 'ID': 'count'})
           .rename(columns={'ID':'COUNT','TIME':'MEAN_TIME'})
           .reset_index()
print (result)
   ID  COUNT  MEAN_TIME
0   1      3   2.666667
1   2      1   3.000000

如果需要按多列聚合的解决方案:

Solution if need aggreagate by multiple columns:

result = df.groupby(['ID']).agg({'TIME':{'MEAN_TIME': 'mean'}, 'ID': {'COUNT': 'count'}})
result.columns = result.columns.droplevel(0)
print (result.reset_index())
   ID  COUNT  MEAN_TIME
0   1      3   2.666667
1   2      1   3.000000

这篇关于值错误:无法插入 ID,已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆