Python 跨多列返回最后日期和名称的值
我有这个1000x6的数据帧。如何添加另外两列:列[latest person],列[latest date]以显示姓氏和姓名 行动日期/时间 输入:Python 跨多列返回最后日期和名称的值,python,pandas,dataframe,Python,Pandas,Dataframe,我有这个1000x6的数据帧。如何添加另外两列:列[latest person],列[latest date]以显示姓氏和姓名 行动日期/时间 输入: Reviewer 3 Action Date / Time.2 Approver 1 Action Date / Time.3 Approver 2 Action Date / Time.4 3970 0 0 Pat McEnt
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
看起来您希望获得最右边的列对,即
最新的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]]
菲尔真是个好主意!我试图得到包含日期的单元格布尔值,但你的更复杂。:)谢谢@Candice,yw,我简化了代码,因为我注意到我的初始表达式有点多余。