[英]Pandas create a conditional column based on cumulative logic operations of the other columns
I have 2 columns that represent the on switch and off switch indicator.我有 2 列代表打开开关和关闭开关指示器。 I want to create a column called last switch where it keeps record the 'last' direction of the switch (whether it is on or off).
我想创建一个名为 last switch 的列,它会在其中记录开关的“最后”方向(无论是打开还是关闭)。 Another condition is that if both on and off switch value is 1 for a particular row, then the 'last switch' output will return the opposite sign of the previous last switch.
另一个条件是,如果特定行的开和关开关值均为 1,则“最后一个开关”输出将返回与前一个最后一个开关相反的符号。 Currently I managed to find a solution to get this almost correct except facing the situation where both on and off shows 1 that makes my code wrong.
目前,我设法找到了一个解决方案,使这几乎是正确的,除非面临开和关都显示 1 使我的代码出错的情况。
I also attached the screenshot with a desired output.我还附上了带有所需输出的屏幕截图。 Please help appreciate all.
请帮助欣赏所有。
df=pd.DataFrame([[1,0],[1,0],[0,1],[0,1],[0,0],[0,0],[1,0],[1,1],[0,1],[1,0],[1,1],[1,1],[0,1]], columns=['on','off'])
df['last_switch']=(df['on']-df['off']).replace(0,method='ffill')
Add the following lines to your existing code:将以下行添加到现有代码中:
for i in range(df.shape[0]):
df['prev']=df['last_switch'].shift()
df.loc[(df['on']==1) & (df['off']==1), 'last_switch']=df['prev'] * (-1)
df.drop('prev', axis=1, inplace=True)
df['last_switch']=df['last_switch'].astype(int)
Output:输出:
on off last_switch
0 1 0 1
1 1 0 1
2 0 1 -1
3 0 1 -1
4 0 0 -1
5 0 0 -1
6 1 0 1
7 1 1 -1
8 0 1 -1
9 1 0 1
10 1 1 -1
11 1 1 1
12 0 1 -1
Let me know if you need expanation of the code如果您需要扩展代码,请告诉我
df=pd.DataFrame([[1,0],[1,0],[0,1],[0,1],[0,0],[0,0],[1,0],[1,1],[0,1],[1,0],[1,1],[1,1],[0,1]], columns=['on','off'])
df['last_switch']=(df['on']-df['off']).replace(0,method='ffill')
prev_row = None
def apply_logic(row):
global prev_row
if prev_row is not None:
if (row["on"] == 1) and (row["off"] == 1):
row["last_switch"] = -prev_row["last_switch"]
prev_row = row.copy()
return row
df.apply(apply_logic,axis=1)
personally i am not a big fan of using loop against dataframe.我个人不是对数据帧使用循环的忠实粉丝。
shift
wont work in this case as the "last_switch" column is dynamic and subject to change based on on&off status.在这种情况下,
shift
不起作用,因为“last_switch”列是动态的,可能会根据开和关状态发生变化。 Using your intermediate reesult with apply
while carrying the value from previous row should do the trick.将中间结果与
apply
一起apply
同时携带上一行的值应该可以解决问题。 Hope it makes sense.希望这是有道理的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.