简体   繁体   English

根据多行计算熊猫中的列

[英]Calculate Columns in Pandas based on multiple rows

I have a dataframe contains following columns: 我有一个数据框包含以下列:

  1. Events 大事记
  2. Transaction ID in which this event happened 发生此事件的交易ID
  3. The Previous transaction ID 先前的交易编号

I want to add a column to calculate if this event happened in the previous transaction or not 我想添加一列以计算此事件是否在上一个事务中发生

items = pd.DataFrame({'event':['A','B','B','A','C','C','C'],
                  'transaction_ID':[1,2,3,4,5,6,7],
                  'previous_trans':[2,3,5,7,4,1,6]})
items["Same_Event_in_prev_trans"]=0

The values of the "Same_Event_in_prev_trans" column should be 0 1 0 0 0 0 1 “ Same_Event_in_prev_trans”列的值应为0 1 0 0 0 0 1

I am not sure how to do that without for loops. 我不确定如何不使用for循环。

Thanks. 谢谢。

You can use lambda to check for the event for previous trans. 您可以使用lambda来检查上一次交易的事件。

items["Same_Event_in_prev_trans"]=(
    items.apply(lambda x: 1 if x.event==items.set_index('transaction_ID')
                                             .loc[x.previous_trans,'event'] 
                            else 0, axis=1)
    )



items
Out[239]: 
  event  previous_trans  transaction_ID  Same_Event_in_prev_trans
0     A               2               1                         0
1     B               3               2                         1
2     B               5               3                         0
3     A               7               4                         0
4     C               4               5                         0
5     C               1               6                         0
6     C               6               7                         1

Not entirely sure about the logic, but checking if each event's previous_trans is within the transaction_ID set seems to give the desired output: 并不完全确定逻辑,但是检查每个事件的previous_trans是否在transaction_ID集合内似乎可以提供所需的输出:

items["Same_Event_in_prev_trans"] = (items.groupby('event', group_keys=False)
                                     .apply(lambda g: g.previous_trans.isin(g.transaction_ID))
                                     .astype(int))
​
items
#   event   previous_trans  transaction_ID  Same_Event_in_prev_trans
#0      A                2              1                       0
#1      B                3              2                       1
#2      B                5              3                       0
#3      A                7              4                       0
#4      C                4              5                       0
#5      C                1              6                       0
#6      C                6              7                       1

How about 怎么样

>> items['prev_event'] = pd.merge(items, items[['event', 'transaction_ID']], 
>>                                left_on='previous_trans', 
>>                                right_on='transaction_ID')['event_y']
>> items['same_event'] = (items['event'] == items['prev_event']).astype(int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM