M Hernandez
M Hernandez

Reputation: 29

Appending status to dataframe in pandas

I want to create a status based off dates but I'm having a problem appending the data. I have the data pulling in from a spreadsheet.

I have tried the append function but it gives me error. I have looked online but I cannot find how to do this.

#import pandas 
import pandas as pd
import numpy as np

#Read Excel Sheet with Data 
df = pd.read_csv('/Users/marvin-nonbusiness/Desktop/SHAREPOINT.csv')


#Show data 
print(pd.isna(df))                       

print('-------------------------------------------------------------------------------------------')

print(df)



#Create Status 
def marvin():
    result = []
    if pd.isna(row['pre boarded ']) == True and  pd.isna(row['post           boarded']) == False and pd.isna(row['remd reqd']) == True and pd.isna(row['sent to clc']) == True and pd.isna(row['review closed']) == True:
    result.append('POST BOARDED STARTED')
elif pd.isna(row['pre boarded ']) == True and  pd.isna(row['post boarded']) == False and pd.isna(row['remd reqd']) == False and pd.isna(row['sent to clc']) == True and pd.isna(row['review closed']) == True:
    result.append('REMEDIATION REQD-PENDING LOG TO CLC')
elif pd.isna(row['pre boarded ']) == True and  pd.isna(row['post boarded']) == False and pd.isna(row['remd reqd']) == False and pd.isna(row['sent to clc']) == False and pd.isna(row['review closed']) == True:
    result.append('REMEDIATION REQD-SENT TO CLC')
elif pd.isna(row['pre boarded ']) == True and  pd.isna(row['post boarded']) == False and pd.isna(row['remd reqd']) == False and pd.isna(row['sent to clc']) == False and pd.isna(row['review closed']) == False:
    result.append('REVIEW COMPLETED-ISSUES FOUND') 
else:
    result.append('DATE EXCEPTION')

df.append(marvin())        
df
print('executed')

Right now there is 4 columns without status.

Expected results would be 5 columns with a status column

Upvotes: 1

Views: 212

Answers (1)

jezrael
jezrael

Reputation: 863301

I believe you need:

#add variable row
def marvin(row):
    result = []
        ...
        ...
    else:
        result.append('DATE EXCEPTION')
    #add return list result 
    return result

#add apply per rows
df['new'] = df.apply(marvin, axis=1)

Yoour solution should be rewritten by numpy.select:

m1 = df['pre boarded'].isna() & df['post boarded'].notna()
m2 = df['remd reqd'].isna()
m3 = df['sent to clc'].isna()
m4 = df['review closed'].isna()

masks = [m1 & m2 & m3 & m4, 
         m1 & ~m2 & m3 & m4, 
         m1 & ~m2 & ~m3 & m4, 
         m1 & ~m2 & ~m3 & ~m4]

values = ['POST BOARDED STARTED',
          'REMEDIATION REQD-PENDING LOG TO CLC',
          'REMEDIATION REQD-SENT TO CLC',
          'REVIEW COMPLETED-ISSUES FOUND']

df['new'] = np.select(masks, values, default='DATE EXCEPTION')

Upvotes: 1

Related Questions