4

我有一张有近 3100 万条记录的表。大约有 10 列,其中两列是卡号和 transaction_status。每张卡片可以有多行。所以可能有 2000 行同一张卡,每行作为具有相应状态的交易

transaction_Status 的值为“Y”/“N”。

我想使用 pandas 数据框在此表中添加另外两列“count_of_approved”、“count_of_rejected”。

我怎么做?到目前为止,我一直在使用 get_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'})
4

3 回答 3

4

您可以使用lambda 表达式,其次数等于the或using :GroupBy card_idtransformsumstatusYnum_approvedNnum_of_deniedeq

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
于 2018-12-25T13:10:12.520 回答
4

使用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
于 2018-12-25T13:15:03.740 回答
1

您可以使用交叉表

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
于 2018-12-25T13:18:19.260 回答