每组值作为单独列的出现计数

2024-09-30 14:20:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张表,有将近3千1百万条记录。 大约有10列,其中两列是卡号和交易状态。每张卡可以有多行。因此,同一张卡可能有2000行,每行作为一个具有相应状态的交易

事务状态的值为“Y”/“N”。你知道吗

我想使用pandas dataframe将另外两列添加到此表中,“count\u of \u approved”、“count\u of \u rejected”。你知道吗

我该怎么做? trn公司 到目前为止,我一直在使用get\u dummies()和merge(),但是这需要很多时间,更糟糕的是,会导致内存不足错误。你知道吗

我的意见如下:

trn_id | card_id | status
1      | c1      | Y
2      | c2      | Y
3      | c2      | N
4      | c3      | Y 
5      | c3      | Y 

我希望我的输出是

trn_id | card_id | status | num_approved | num_of_denied
1      | c1      | Y      | 1            | 0
2      | c2      | Y      | 1            | 1
3      | c2      | N      | 1            | 1
4      | c3      | Y      | 2            | 0
5      | c3      | Y      | 2            | 0

我的代码如下:

import pandas as panda
a = panda.DataFrame({'id':[1,2,3],'c_id':[22,22,33], 'status':['Y','Y','N']})
temp = a.status.str.get_dummies()
a[['N','Y']]= temp
tt = a.groupby(['c_id'])['Y'].count()
tt=tt.reset_index()
yes_count_added = a.merge(tt,how='right',on='c_id')
yes_count_added.rename(columns = {'Y_y':'num_of_approved'})

Tags: ofidpandasget状态statuscount交易
3条回答

您可以^{}card_id并使用^{}和lambda表达式sum使用status的次数等于Ynum_approvedNnum_of_denied^{}

df['num_approved'] = df.groupby('card_id').status.transform(
                                lambda x: x.eq('Y').sum())
df['num_of_denied'] = df.groupby('card_id').status.transform(
                                 lambda x: x.eq('N').sum())

     trn_id card_id  status    num_approved    num_of_denied
0       1      c1      Y             1              0
1       2      c2      Y             1              1
2       3      c2      N             1              1
3       4      c3      Y             2              0
4       5      c3      Y             2              0

使用str.get_dummies+单个groupby调用来提高性能:

df.status.str.get_dummies().groupby(df.card_id).transform('sum')

   N  Y
0  0  1
1  1  1
2  1  1
3  0  2
4  0  2

v = (df.status
       .str.get_dummies()
       .groupby(df.card_id)
       .transform('sum')
       .rename({'Y': 'num_approved', 'N': 'num_denied'}, axis=1))

pd.concat([df, v], axis=1)

   trn_id card_id status  num_denied  num_approved
0       1      c1      Y           0             1
1       2      c2      Y           1             1
2       3      c2      N           1             1
3       4      c3      Y           0             2
4       5      c3      Y           0             2

您可以使用crosstab

import pandas as pd

a = pd.DataFrame(
    {'trn_id': [1, 2, 3, 4, 5],
     'card_id': ['c1', 'c2', 'c2', 'c3', 'c3'],
     'status': ['Y', 'Y', 'N', 'Y', 'Y']})

crosstab = pd.crosstab(a.card_id, a.status).reset_index(level=0).rename(
    columns={'Y': 'num_approved', 'N': 'num_denied'})
print(pd.merge(a, crosstab, on='card_id'))

输出

  card_id status  trn_id  num_denied  num_approved
0      c1      Y       1           0             1
1      c2      Y       2           1             1
2      c2      N       3           1             1
3      c3      Y       4           0             2
4      c3      Y       5           0             2

相关问题 更多 >