熊猫-如何遍历groupby以计算发生次数

时间:2020-05-24 13:49:04

标签: python pandas dataframe dictionary pandas-groupby

我有一个类似下面的DF:

我要对价格进行分组,计算每个价格的动作== 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中。

1 个答案:

答案 0 :(得分: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