将状态附加到熊猫中的数据框

时间:2019-01-18 06:08:50

标签: python pandas

我想根据休假日期创建状态,但附加数据时遇到问题。我有从电子表格中提取的数据。

我尝试了append函数,但它给了我错误。我已经看过网上了,但是找不到如何做。

#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')

现在有4列没有状态。

预期结果将为5列,其中包含一个状态列

1 个答案:

答案 0 :(得分:1)

我相信您需要:

#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)

您的解决方案应由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')