python - 将状态附加到 pandas 中的数据框

标签 python pandas

我想创建一个基于截止日期的状态,但在附加数据时遇到问题。我从电子表格中提取数据。

我尝试了附加功能,但它给了我错误。我在网上查过,但找不到如何执行此操作。

#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 列,其中有一个状态列

最佳答案

我相信你需要:

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

关于python - 将状态附加到 pandas 中的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54248557/

相关文章:

python - 在一个窗口中执行具有多个屏幕的项目的最佳 Python GUI 模块是什么?

python - 在python中导入字典

python - 计算每组中 NaN 的数量

python - 在 Python 中的 pandas 数据框上创建通用数据过滤器

python - 将 rank 2 numpy 数组分配给 pandas DataFrame 列的行为不一致

python - 无法创建 Django 模型的实例

python - SymPy 矩阵参数不可比较

python - 如何防止Pycharm格式化一行HTML代码

python - 跨多个数据框的 bool 比较

python - 如何使用数据框中所有可能的值扩展占位符 '*'?