Python 基于条件添加行-数据帧

Python 基于条件添加行-数据帧,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据框,如下所示: 我想根据以下逻辑添加新行: 添加“位置”为“舞台区域”的新行 此行是条目的总和,其中“位置”是“回复区-新商业区” 以及“地点”为“文化中心”的条目 把这些行放下 “位置”为“回复区-新商业区”和“文化区” “中心” 因此,对于2020年11月11日,我应该有以下条目: 用于按多个值筛选、聚合和添加列位置和最后添加到原始数据帧,而不使用掩码匹配行: mask = df['location'].isin(["Reply's Area - New Commer

我有一个数据框,如下所示:

我想根据以下逻辑添加新行:

  • 添加“位置”为“舞台区域”的新行
  • 此行是条目的总和,其中“位置”是“回复区-新商业区” 以及“地点”为“文化中心”的条目
  • 把这些行放下 “位置”为“回复区-新商业区”和“文化区” “中心”
  • 因此,对于2020年11月11日,我应该有以下条目:

    用于按多个值筛选、聚合和添加列
    位置
    和最后添加到原始数据帧,而不使用掩码匹配行:

    mask = df['location'].isin(["Reply's Area - New Commercial Area", 'Cultural Hub'])
    
    df1 = (df[mask].groupby(['day','locationTypes'],as_index=False)[['dwell', 'football']]
                  .sum()
                  .assign(location = 'Stage Area')
                  .reindex(df.columns, axis=1))
    
    df = pd.concat([df[~mask], df1], ignore_index=True)
    
    用于按多个值筛选、聚合和添加列
    位置
    和最后添加到原始数据帧,而不使用掩码匹配行:

    mask = df['location'].isin(["Reply's Area - New Commercial Area", 'Cultural Hub'])
    
    df1 = (df[mask].groupby(['day','locationTypes'],as_index=False)[['dwell', 'football']]
                  .sum()
                  .assign(location = 'Stage Area')
                  .reindex(df.columns, axis=1))
    
    df = pd.concat([df[~mask], df1], ignore_index=True)
    

    耶兹雷尔看起来很接近答案,但也许足球上的聚合是不正确的。。。只是看了他的代码,所以我可能错了

    正确的版本如下所示,这与您在示例中建议的数字相匹配。 我制作了一个较小版本的示例表,用于测试。这里的“数据”是您的数据帧

    mask = data["location"].isin(["Repley's Area - New Commercial Area", "Cultural Hub"])
    data[mask].groupby(["day","locationTypes"], as_index=False)['dwell', 'football'].sum().assign(location="Stage Area")
    
    输出:

              day locationTypes  dwell  football    location
    0  2020-11-11          Zone    145      2307  Stage Area
    1  2020-11-12          Zone     95      2905  Stage Area
    

    耶兹雷尔看起来很接近答案,但也许足球上的聚合是不正确的。。。只是看了他的代码,所以我可能错了

    正确的版本如下所示,这与您在示例中建议的数字相匹配。 我制作了一个较小版本的示例表,用于测试。这里的“数据”是您的数据帧

    mask = data["location"].isin(["Repley's Area - New Commercial Area", "Cultural Hub"])
    data[mask].groupby(["day","locationTypes"], as_index=False)['dwell', 'football'].sum().assign(location="Stage Area")
    
    输出:

              day locationTypes  dwell  football    location
    0  2020-11-11          Zone    145      2307  Stage Area
    1  2020-11-12          Zone     95      2905  Stage Area
    

    谢谢你的回复!以下方面发挥了作用:

    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')]
    
    #给


    谢谢您的回复!以下方面发挥了作用:

    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')]
    
    #给


    Oki,增加了列变更单的
    reindex
    ,增加了列变更单的
    reindex