mask=df[df['location'].isin(["Repley's Area - New Commercial Area",'Cultural Hub'])]
df1=mask.groupby(['day','locationTypes'],as_index=False)['footfall','dwell (minutes)'].sum().assign(location='Stage Area')
#reordering the columns for pd.concat
df1= df1[df.columns]
df_final=pd.concat([df[~df['location'].isin(["Repley's Area - New Commercial Area",'Cultural Hub'])],df1])
#checking the result
df_final[(df_final['day']=='2020-11-11') & (df_final['location']=='Stage Area')]
耶兹雷尔看起来很接近答案,但也许足球上的聚合是不正确的。。。只是看了他的代码,所以我可能错了
正确的版本如下所示,这与您在示例中建议的数字相匹配。 我制作了一个较小版本的示例表,用于测试。这里的“数据”是您的数据帧
输出:
谢谢你的回复!以下方面发挥了作用:
#哪一个给了你
使用^{} 按多个值进行筛选,聚合和添加列
location
,最后添加到原始数据帧,不使用掩码匹配行:相关问题 更多 >
编程相关推荐