Reputation: 1117
print (df)
Event1 Event2 Event3
0 7/27/2014 NaN NaN
1 3/5/2016 NaN 2/1/2013
2 5/13/2017 5/10/2013 NaN
3 NaN NaN 4/28/2014
4 NaN 5/12/2013 3/6/2016
5 NaN NaN NaN
Hi all,
I have 3 columns in my pandas dataframe, I want to create a new column to show the minimum date among Event 1
, Event 2
, and Event 3
. If there is no date in all the 3 columns, then print NaN
as shown in the last row of the screenshot above.
The last column in the screenshot (New column) is the expected output. Any advise or help will be greatly appreciated!
Upvotes: 1
Views: 407
Reputation: 862441
Filter columns with Event
with DataFrame.filter
, convert to datetimes and get minimal values, last for original format use Series.dt.strftime
:
df['new'] = (df.filter(like='Event')
.apply(pd.to_datetime)
.min(axis=1)
.dt.strftime('%m/%d/%Y'))
print (df)
Event1 Event2 Event3 new
0 7/27/2014 NaN NaN 07/27/2014
1 3/5/2016 NaN 2/1/2013 02/01/2013
2 5/13/2017 5/10/2013 NaN 05/10/2013
3 NaN NaN 4/28/2014 04/28/2014
4 NaN 5/12/2013 3/6/2016 05/12/2013
5 NaN NaN NaN NaN
Upvotes: 3