【问题标题】:Aggregating on the aggregated values in pandas returns wrong result对 pandas 中的聚合值进行聚合会返回错误的结果
【发布时间】:2018-09-05 11:45:35
【问题描述】:

我有这样的dataframe - 每笔交易可能出现多个,并且交易与商店相关联。我想找到交易的平均价值。为此,我需要求和然后找到平均值:

#preparind dataset
txt_data = pandas.read_csv("./TestDataSource/txn.csv", sep = ';')
txt_data = txt_data.replace({',': '.'}, regex=True)
txt_data[['SALES']] = txt_data[[ 'SALES']].apply(pd.to_numeric)

len(txt_data.STORE.unique()) 这里只有 30 个独特的商店。

首先我汇总交易:

a1 = txt_data[['STORE', 'SALES', 'TXN']].groupby('TXN').sum()[['STORE', 'SALES']]
a.head()

一切似乎都很好。但后来我汇总了商店:

a2 = a1.groupby('STORE').mean()
[![enter image description here][3]][3]

但是…… list(a2.shape) - 返回 [1137, 1] 。这真是令人困惑。但此外len(a1.STORE.unique()) 返回 1137

我做错了什么

【问题讨论】:

    标签: python python-3.x pandas pandas-groupby


    【解决方案1】:

    我认为你的问题是在你使用这条线时发生的,

    a1 = txt_data[['STORE', 'SALES', 'TXN']].groupby('TXN').sum()
    

    当我使用 txt_data['STORE'].unique() 获得唯一值时,它会输出,

    array([22691, 20581,  1574,  1602,  1579, 29245, 19009, 21761, 17474,
            1544,  1612,  1534,   958, 17096,  1094,  1596,  1594,  1609,
           24605,   956,   961,  1122, 27220,   974,  1082, 25039,  1530,
             999,  1053,   980])
    

    但是在 a1 数据框中, STORE 值与 txt_data 不同,因为 group_by.sum() 对 STORE 中的值求和以获得唯一的 'TXN'。

    见:txt_data['STORE'].unique()中没有STORE = 4328

    1082 * 4 = 4328

    【讨论】:

      【解决方案2】:

      您按sum 每个TXN 列聚合STORESALES 列存在问题:

      a1 = txt_data[['STORE', 'SALES', 'TXN']].groupby('TXN').sum()[['STORE', 'SALES']]
      

      什么是相同的:

      a1 = txt_data.groupby('TXN')['STORE', 'SALES'].sum()
      

      但如果按列聚合 TXTSTORE 一切都很好:

      txt_data = pd.read_csv("txn.csv", sep = ';', decimal=',')
      
      a1 = txt_data.groupby(['TXN', 'STORE'], as_index=False)['SALES'].sum()
      
      print (txt_data.STORE.nunique())
      30
      
      print (a1.STORE.nunique())
      30
      

      【讨论】:

        【解决方案3】:

        排队

        a1 = txt_data[['STORE', 'SALES', 'TXN']].groupby('TXN').sum()[['STORE', 'SALES']]
        

        您正在按 TXN 对数据框进行分组,但告诉 pandas 对所有其他列求和,这样您就可以得到 store-ids 的总和和“创建的新商店”,例如:

        txt_data[txt_data['TXN']==5541359000]  
        
                       DAY  STORE   ART                    TXN      TIME    SALES
        1268877 2015-10-01  1082    15294488        5541359000  09:30:22    60.2
        1269093 2015-10-01  1082    80439           5541359000  09:30:29    15.6
        1269309 2015-10-01  1082    191452          5541359000  09:30:15    4.0
        1269525 2015-10-01  1082    15317962        5541359000  09:30:17    103.0
        
        a1.head()
                   STORE    SALES
        TXN     
        5541359000  4328    182.8
        
        #1082 * 4 = 4328
        

        【讨论】:

          猜你喜欢
          • 2015-10-03
          • 1970-01-01
          • 2019-07-20
          • 2021-02-03
          • 1970-01-01
          • 2018-06-20
          • 1970-01-01
          • 2012-11-08
          • 1970-01-01
          相关资源
          最近更新 更多