我有一个有数百万个账户的熊猫数据框架。数据帧有一个标题为“eventochappers”的列,在事件发生的时段中等于1。举个例子:
import pandas as pd
account = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3]
period = [1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 1, 2, 3, 4, 5, 1, 2, 3]
eventHappens = [0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0]
willHappenIn2 = [0, 1, 1, 1, -1, -1, -1, -1, -1, -1, 0, 0, 1, 1, 1, 0, 0, 0]
df = pd.DataFrame(
{'account': account ,
'period': period,
'eventHappens': eventHappens
})
print(df)
account period eventHappens
0 1 1 0
1 1 2 0
2 1 3 0
3 1 4 1
4 1 5 0
5 1 6 0
6 1 8 0
7 1 9 0
8 1 10 1
9 1 11 0
10 2 1 0
11 2 2 0
12 2 3 0
13 2 4 0
14 2 5 1
15 3 1 0
16 3 2 0
17 3 3 0
我想创建一个新的列“willHappenIn2”,除了在第一次发生事件日期之前和发生事件日期时的2个时段外,该列将全部为零,并将其编码为1(我保留-1,以备以后检查数据后事件的特征)。事件日期之后的时段需要编码为-1。这是我想要的结果。有没有办法做到这一点,而不必循环通过每个帐户?你知道吗
account period eventHappens willHappenIn2
0 1 1 0 0
1 1 2 0 1
2 1 3 0 1
3 1 4 1 1
4 1 5 0 -1
5 1 6 0 -1
6 1 8 0 -1
7 1 9 0 -1
8 1 10 1 -1
9 1 11 0 -1
10 2 1 0 0
11 2 2 0 0
12 2 3 0 1
13 2 4 0 1
14 2 5 1 1
15 3 1 0 0
16 3 2 0 0
17 3 3 0 0
更新:
下面的答案提出了以下解决方案(下面显示的答案输出与我想要的匹配)。但是,当我在我的机器上运行它时,输出与我期望的结果不匹配。有什么建议吗?你知道吗
df.loc[df.loc[df.eventHappens==1].groupby('account').eventHappens.idxmax(),'key']=1
s1=df.key.where(df.key==1).groupby(df.account).bfill(2) # using groupby with bfill get the 1 foward with limit 2
s2=(-df.key.where(df.key==1)).groupby(df.account).ffill() # adjust the key and get the back adjust to -1
s1.fillna(s2).fillna(0)
Out[110]:
0 0.0
1 -1.0
2 -1.0
3 1.0
4 -1.0
5 1.0
6 0.0
7 0.0
8 1.0
9 0.0
10 0.0
11 0.0
12 1.0
13 1.0
14 1.0
15 0.0
16 0.0
17 0.0
更新2: 以下方法奏效了。我不得不用replace()和方法“bfill”将bfill方法更改为.apply()。它没有第一个答案那么漂亮(作为我的指导,谢谢!)但它在我的机器上工作。你知道吗
df["willHappenIn2_step1"] = df.eventHappens.groupby(df['account']).cumsum().astype(int)
df["willHappenIn2_step2"] = df.willHappenIn2_step1.groupby(df['account']).cumsum().astype(int)
df["willHappenIn2_step3"] = df.willHappenIn2_step2
df.loc[df.willHappenIn2_step2>1,"willHappenIn2_step3"] = -1
df['Final_OutPut'] = df["willHappenIn2_step3"].groupby(df.account).apply( lambda x: x.replace(to_replace=0, method='bfill', limit = 2))
df["willHappenIn2Desired"] = willHappenIn2
print(df)
我们需要使用
idxmax
after过滤器来创建新的key
,因为同一组中的两个1不会导致输出更改,所以我们只得到第一个值的index
,显示为1以下方法奏效了。我不得不用replace()和方法“bfill”将bfill方法更改为.apply()。它没有第一个答案那么漂亮(作为我的指导,谢谢!)但它在我的机器上工作。你知道吗
相关问题 更多 >
编程相关推荐