Search code examples
pythonpandasfor-loopdata-manipulationnested-if

Using a for loop combined with a nested if statement to create a new pandas DataFrame based on 3 columns of a different DataFrame in Python


trv_last = []
for i in range(0,len(trv)):
    if (trv_name_split.iloc[i,3] != None):
        trv_last = trv_name_split.iloc[i,3]
    elif (trv_name_split.iloc[i,2] != None):
        trv_last = trv_name_split.iloc[i,2]
    else: 
        trv_last = trv_name_split.iloc[i,1]
        
trv_last

This returns 'Campo' which is the last index in my range:

     0         1     2        3
  1 John    Doe     None    None
  2 Jane    K.      Martin  None
  :   :      :       :       :
972 Jino    Campo   None    None

As you can see all names were together in one column and I used str.split() to split them up. Since some names had first middle middle last, I am left with 4 columns. I am only interested in he last name.

My goal is to create a new DF with only the last name. The logic here is if the 4th column is not "None" then that is the last name and move backwards toward the 2nd column being last name if all else are "None".

Thank you for having a look and I appreciate the help!


Solution

  • Figured out the answer to my own question..

    trv_last = []
    for i in range(0,len(trv)):
        if (trv_name_split.iloc[i,3] != None):
            trv_last.append(trv_name_split.iloc[i,3])
        elif (trv_name_split.iloc[i,2] != None):
            trv_last.append(trv_name_split.iloc[i,2])
        else: 
            trv_last.append(trv_name_split.iloc[i,1])
            
    trv_last