我有这个1000x6数据帧。如何添加另外两列:column [latest person],column [latest date]以显示姓氏和 动作日期/时间?
输入:
Reviewer 3 Action Date / Time.2 Approver 1 Action Date / Time.3 Approver 2 Action Date / Time.4
3970 0 0 Pat McEntee 10.04.2018 - 11:01:55 0 0
3971 John Hunt 12.04.2018 - 17:07:54 Paul Monnington 24.04.2018 - 16:22:17 0 0
3974 0 0 Mike Smith 16.04.2018 - 09:30:06 0 0
3975 0 0 Peter McNamara 13.04.2018 - 16:15:27 Stephen Harrison 20.04.2018 - 11:32:39
3977 0 0 Ananth Sarathy 17.04.2018 - 17:37:03 0 0
3978 John Hunt 19.04.2018 - 09:08:12 Sandra Osteni 19.04.2018 - 09:12:38 Amanda Bardwell 01.05.2018 - 09:01:20
3979 John Hunt 19.04.2018 - 09:08:12 Sandra Osteni 19.04.2018 - 09:09:36 Amanda Bardwell 01.05.2018 - 09:02:08
3980 John Hunt 19.04.2018 - 09:08:12 Sandra Osteni 19.04.2018 - 09:13:01 Amanda Bardwell 01.05.2018 - 09:00:49
3981 John Hunt 17.04.2018 - 15:52:58 Sandra Osteni 19.04.2018 - 09:11:58 Amanda Bardwell 01.05.2018 - 09:00:17
3982 John Hunt 17.04.2018 - 15:52:01 Sandra Osteni 19.04.2018 - 09:11:22 Amanda Bardwell 01.05.2018 - 09:01:34
3984 0 0 Mike Smith 04.05.2018 - 14:51:58 0 0
...
4012 0 0 Trent Mason 23.04.2018 - 12:56:04 Mike Smith 23.04.2018 - 15:03:06
4013 0 0 Trent Mason 23.04.2018 - 12:57:26 Mike Smith 23.04.2018 - 15:14:41
4014 0 0 Trent Mason 23.04.2018 - 12:56:58 Mike Smith 23.04.2018 - 15:15:46
4015 0 0 Trent Mason 23.04.2018 - 12:56:45 Mike Smith 23.04.2018 - 15:05:52
4016 0 0 Trent Mason 23.04.2018 - 12:57:40 Mike Smith 23.04.2018 - 15:03:39
4017 0 0 Trent Mason 23.04.2018 - 12:55:14 Mike Smith 23.04.2018 - 15:04:52
4018 John Hunt 23.04.2018 - 10:29:59 Sandra Osteni 30.04.2018 - 13:09:01 Sandra Osteni 30.04.2018 - 13:09:19
4019 0 0 Trent Mason 23.04.2018 - 12:55:50 Mike Smith 23.04.2018 - 15:09:18
所需的输出:
Latest person Latest date
Pat McEntee 10.04.2018 - 11:01:55
Paul Monnington 24.04.2018 - 16:22:17
Mike Smith 16.04.2018 - 09:30:06
Stephen Harrison 20.04.2018 - 11:32:39
Ananth Sarathy 17.04.2018 - 17:37:03
...
Sandra Osteni 30.04.2018 - 13:09:19
Mike Smith 23.04.2018 - 15:09:18
答案 0 :(得分:1)
您似乎想将最右边的列对作为Latest person/date
。为了快速实现此目的,在将0
替换为NaN
之后,请按列进行前向填充,并为新列获取最右边的信息。
import numpy as np
df = df.replace("0", np.nan)
persons = df[df.columns[::2]].fillna(axis=1, method='ffill')
dates = df[df.columns[1::2]].fillna(axis=1, method='ffill')
df['Latest person'] = persons[persons.columns[-1]]
df['Latest date'] = dates[dates.columns[-1]]