gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:26:02 25 4
gpt4 key购买 nike

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com