【问题标题】:Pandas - How to iterate over groupby to count number of occurencesPandas - 如何迭代 groupby 以计算出现次数
【发布时间】:2020-09-11 04:12:32
【问题描述】:

我有一个像下面这样的 DF:

我想按价格分组,计算每个价格出现 action == N / U / D 的次数。

ID,Action,indicator,side, price, quantity
7930249,U,0,A,132.938,23
7930251,D,0,B,132.906,2
7930251,N,1,B,132.891,36
7930251,U,0,A,132.938,22
7930252,U,0,A,132.938,2
7930252,U,1,A,132.953,39
7930252,U,2,A,132.969,17
7930514,U,0,B,132.906,1
7930514,U,0,A,132.938,8
7930514,U,1,A,132.953,38
7930515,U,0,A,132.938,18
7930515,U,2,A,132.969,7
7930516,U,1,B,132.891,37
7930516,U,0,A,132.938,28

当前代码:

pricelist = []
column_names = ['Price', 'N', 'U', 'D']
df_counter = pd.DataFrame(columns = column_names)

for name, group in df.groupby('Price'):
    price = name
    if price not in pricelist:
        pricelist.append(price)
        n_count = group['Action'][group['Action']=='N'].count()
        u_count = group['Action'][group['Action']=='U'].count()
        d_count = group['Action'][group['Action']=='D'].count()
        dflist = [price, n_count, u_count, d_count]
        price_dict = {'Price':price,'N':n_count, 'U':u_count,'D':d_count}
        df1 = pd.DataFrame([price_dict], columns=price_dict.keys())
        result = df_counter.append(df1)
        continue
    else:
        continue

返回:

Price   N   U   D
0   136.938 1   0   0

为什么不创建更长的数据框?我基本上得到了打印 price_dict 的结果,但是,我正在努力将其保存到 Dataframe。

【问题讨论】:

    标签: python pandas dataframe dictionary pandas-groupby


    【解决方案1】:

    IIUC,尝试使用pd.crosstab,而不是编写自己的方法:

    pd.crosstab(df['price'], df['Action'])
    

    输出:

    Action   D  N  U
    price           
    132.891  0  1  1
    132.906  1  0  1
    132.938  0  0  6
    132.953  0  0  2
    132.969  0  0  2
    

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 2020-06-20
      • 1970-01-01
      相关资源
      最近更新 更多