简体   繁体   English

Pandas 根据其他列的累积逻辑运算创建条件列

[英]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.

相关问题 Pandas 根据许多其他列的条件逻辑添加一个新列 - Pandas add a new column based on conditional logic of many other columns Pandas DataFrame 基于依赖于具有累积计数规则的其他列的逻辑创建新列 - Pandas DataFrame create new columns based on a logic dependent on other columns with cumulative counting rule Pandas:如何根据其他列值的条件创建对其他列求和的列? - Pandas: How create columns where sum other columns based on conditional of other column values? 根据其他列的条件创建新列 - create new column based on conditional of other columns 获取以其他列为条件的累计熊猫总和 - Get cumulative sum Pandas conditional on other column 如何在Pandas Data Frame中创建条件列,其中列值基于其他列 - How to create conditional columns in Pandas Data Frame in which column values are based on other columns 在其他列之间创建具有各种条件逻辑的新列 - Create new column with various conditional logic between other columns 根据所有其他列的条件计数创建新的 Pandas 列 - Create new Pandas columns based on conditional counts of all other columns 根据来自其他两列的条件文本值在 Pandas 中创建一个新列 - Create a new column in pandas based on conditional text values from two other columns 如何基于一个或多个 OTHER 列的条件子字符串搜索在 Pandas 数据框中创建一列 - How to create a column in a Pandas dataframe based on a conditional substring search of one or more OTHER columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM