Python Pandas-如何迭代groupby以计算发生次数

Python Pandas-如何迭代groupby以计算发生次数,python,pandas,dataframe,dictionary,pandas-groupby,Python,Pandas,Dataframe,Dictionary,Pandas Groupby,我的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,13

我的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

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

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

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