为了账号安全,请及时绑定邮箱和手机立即绑定

确定错误的值并在列之间移动它们

确定错误的值并在列之间移动它们

冉冉说 2022-11-29 15:27:14
这是我简化的 CSV 样本数据:Project;Step 1 Start;Step 1 End;Step 2 Start;Step 2 End;Step 3 Start;Step 3 End;Step 4 Start;Step 4 End;Step 5 Start;Step 5 End;FinishA;01.01.2020;02.01.2020;02.01.2020;03.01.2020;03.01.2020;04.01.2020;04.01.2020;05.01.2020;05.01.2020;06.01.2020;06.01.2020B;01.02.2020;02.02.2020;02.02.2020;03.02.2020;;;;;03.02.2020;04.02.2020;04.02.2020C;01.03.2020;02.03.2020;02.03.2020;;;;;;;03.03.2020;03.03.2020D;01.04.2020;;;;;;;;;02.04.2020;02.04.2020E;01.05.2020;02.05.2020;02.05.2020;03.05.2020;;;03.05.2020;;;04.05.2020;04.05.2020读取文件:import pandas as pddf = pd.read_csv("sampledata.csv", sep=";")数据结构:Project Step 1 Start  Step 1 End Step 2 Start  Step 2 End Step 3 Start  \0       A   01.01.2020  02.01.2020   02.01.2020  03.01.2020   03.01.2020   1       B   01.02.2020  02.02.2020   02.02.2020  03.02.2020          NaN   2       C   01.03.2020  02.03.2020   02.03.2020         NaN          NaN   3       D   01.04.2020         NaN          NaN         NaN          NaN   4       E   01.05.2020  02.05.2020   02.05.2020  03.05.2020          NaN      Step 3 End Step 4 Start  Step 4 End Step 5 Start  Step 5 End      Finish  0  04.01.2020   04.01.2020  05.01.2020   05.01.2020  06.01.2020  06.01.2020  1         NaN          NaN         NaN   03.02.2020  04.02.2020  04.02.2020  2         NaN          NaN         NaN          NaN  03.03.2020  03.03.2020  3         NaN          NaN         NaN          NaN  02.04.2020  02.04.2020  4         NaN   03.05.2020         NaN          NaN  04.05.2020  04.05.2020每一行代表一个项目每个项目可以运行不同的步骤(在这个简化的示例中只有 5 个项目和 5 个步骤。原始数据有超过 100k 个项目和 40 个步骤)每个步骤都有两列。项目进入步骤时为“开始”列,项目离开步骤时为“结束”列每个项目都以“第 1 步开始”开始,以“完成”结束,但并非必须遵循每个步骤我的问题:有时“结束”时间戳写在错误的列中,我必须用 python 更正它。当完成一个项目时,最后一个“结束”时间戳写在“Step 5 End”而不是最后一步“Step X End”。如何确定错误的值如果“Finish”和“Step 5 End”包含时间戳并且“Step 5 Start”为 NaN。如何更正错误的值如果“Start”和“End”包含值,请检查从 1 到 n 的每个步骤。如果“Start”包含一个值而“End”为空,则将时间戳从“Step 5 End”移动到“Step X End”
查看完整描述

1 回答

?
眼眸繁星

TA贡献1873条经验 获得超9个赞

你可以用


# get where the starts are not empty

arr_start = df.filter(like='Start').notna().to_numpy()

# get the end columns

df_end = df.filter(like='End')

# fillna the right missing value

df = df.fillna(df_end.bfill(axis=1)[arr_start&df_end.isna()])

# remove the values of the last step end if no value in the start of this step

df.loc[~arr_start[:,-1], df_end.columns[-1]] = np.nan


print (df)

  Project Step 1 Start  Step 1 End Step 2 Start  Step 2 End Step 3 Start  \

0       A   01.01.2020  02.01.2020   02.01.2020  03.01.2020   03.01.2020   

1       B   01.02.2020  02.02.2020   02.02.2020  03.02.2020          NaN   

2       C   01.03.2020  02.03.2020   02.03.2020  03.03.2020          NaN   

3       D   01.04.2020  02.04.2020          NaN         NaN          NaN   

4       E   01.05.2020  02.05.2020   02.05.2020  03.05.2020          NaN   


   Step 3 End Step 4 Start  Step 4 End Step 5 Start  Step 5 End      Finish  

0  04.01.2020   04.01.2020  05.01.2020   05.01.2020  06.01.2020  06.01.2020  

1         NaN          NaN         NaN   03.02.2020  04.02.2020  04.02.2020  

2         NaN          NaN         NaN          NaN         NaN  03.03.2020  

3         NaN          NaN         NaN          NaN         NaN  02.04.2020  

4         NaN   03.05.2020  04.05.2020          NaN         NaN  04.05.2020  

多一个解释,arr_start&df_end.isna()允许发现哪个步骤有开始而不是结束,使用bfill将有助于获得最后一步结束到需要填充的结束步骤的值


查看完整回答
反对 回复 2022-11-29
  • 1 回答
  • 0 关注
  • 54 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信