Home > Net >  Python Script: Extract Only if All Conditions Are Not Met
Python Script: Extract Only if All Conditions Are Not Met

Time:09-10

The following is an extract of some test data:

APNTPLID APNTPLID EPSLOCK LOCK_S UPL UPLP LOCK_MSIN LOCK_MSOUT OCSI TCSI
6&1&2 6&1&2 EPSLOCK 1 1 1 1 1 YES YES
42&1&101&6&2 42&1&101&6&2 EPSLOCK 1 1 1 1 1 YES YES
2&6&101 2&6&101 EPSLOCK 1 1 1 0 1 YES YES
2&101&6 2&101&6 0 1 1 1 1 1 YES YES
2&6&101 2&6&101 EPSLOCK 1 1 0 1 1 YES YES

I have attempted, using Python, to extract the required columns ONLY if a record is Activated (NOT Deactivated). A record is deemed Deactivated ONLY when the following values occurs for the specific fields for each record, and is all present at the same time:

 - EPSLOCK=EPSLOCK
 - LOCK_S=1
 - UPL=1
 - UPLP=1
 - LOCK_MSIN=1
 - LOCK_MSOUT=1

Therefore, after running the script only the last 3 rows should be extracted - since the first 2 rows are Deactivated records.

I have tried the following Script but it extracts the Deactivated as well. Any assistance appreciated. (Really new to this)

import pandas as pd

file_path = "C:\\Users\\user1\\data\\testdata.csv"
data = pd.read_csv(file_path)

data = data[(data["EPSLOCK"] == "EPSLOCK") |  (data["EPSLOCK"] == 0) | (data["LOCK_S"] == 1) | (data["LOCK_S"] == 0) | (data["UPL"] == 1) | (data["UPL"] == 0) | (data["UPLP"] == 1) | (data["UPLP"] == 0) | (data["LOCK_MSIN"] == 1) | (data["LOCK_MSIN"] == 0) | (data["LOCK_MSOUT"] == 1)  | (data["LOCK_MSOUT"] == 0)]
data = data[["APNTPLID", "EPSLOCK", "APNTPLID", "LOCK_S", "UPL", "UPLP", "LOCK_MSIN", "LOCK_MSOUT", "OCSI", "TCSI"]]

data.to_csv("C:\\Users\\user1\\testresult.csv")
print(data)

CodePudding user response:

It seems to me that you are listing pretty much every possible combination. Also, you use | (or) instead of & (and) as all conditions need to be true simultaneously.

Based on your conditions of when something is deactivated, this would work:

data = df[~((df["EPSLOCK"] == "EPSLOCK") 
          & (df["LOCK_S"] == 1) 
          & (df["UPL"] == 1) 
          & (df["UPLP"] == 1)  
          & (df["LOCK_MSIN"] == 1) 
          & (df["LOCK_MSOUT"] == 1))]

in which ~ means that it should not be true.

  • Related