简体   繁体   中英

How did I loop through each row's value in Pandas df to check if that value satisfies a condition

I have a table as follows:

ID   IsCreated  ParentID
101  0          NA
102  1          101
103  0          NA
104  1          102
105  1          104

I want to add a new col called 'OGParentID' which looks results as follows:

ID   IsCreated  ParentID   OGParentID
101  0          NA         101        
102  1          101        101        
103  0          NA         103        
104  1          102        101        
105  1          104        101  

  

The logic used here is as follows: For each row, check if IsCreated = 1, if yes, then lookup the ParentID in the ID columns to check if it's IsCreated is 0, if yes then set OGParentID to the looked up Parent ID else, check the looked up ParentID's Parent ID and continue the loop.

You can do:

#creating a dictionary "x" to have all the unique parent-child relationships using comprehension
x={i:int(j) for i,j in zip(df['ID'],df['ParentID'].fillna(df['ID']))}

#assigning ID to the new column 
df['OGParentID']=df['ID']

#loop to replace the dict items ,i.e, replacing all the childs with parents
#counter is limited to the length of unique elements in dict
i=0
while i<=len(x):
    df['OGParentID']=df['OGParentID'].replace(x)
    i+=1


Output: 

        ID  IsCreated  ParentID  OGParentID
    0  101          0       NaN         101
    1  102          1     101.0         101
    2  103          0       NaN         103
    3  104          1     102.0         101
    4  105          1     104.0         101

You could write a function and then apply it to the column

def lookup(parent_id):
    while True:
        # find the row where ID is equal to the parent_id
        temp = df[df[‘ID’]==parent_id]
        # check if iscreated is 0, return the parent_id if it is 0
        if temp[‘IsCreated’][0]==0:
            return parent_id
        # at this point, iscreated must be 1, so now we set parent_id to the ParentID in this row and do it again until we find iscreated is 0.
        parent_id = temp[‘ParentID’]

df[‘OGParentID’] = df[‘ParentID’].apply(lookup)


This code will take each ParentID and feed it to the function lookup .

The function lookup does the following as per OP's requirement:

  1. Find the row where ID is equal to the given parent_id
  2. Check if iscreated is 0. If so, return the ParentID of this row
  3. If not, set parent_id to the ParentID of this row and repeat step 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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