I am working with data frame. One of the columns contains date where the format of cell are mixed between date, time and string. The sample date frame is as follow:
df = pd.DataFrame() df['Date'] = ['EPL','Sunday 21st April 2019','13:30:00','13:30:00','French Ligue 1','14:30:00'] df['Name'] = ['A','B','C','D','E','F']
I want to create a new column that contains the text of date column. The expected output is as follows:
How can I do that?
Advertisement
Answer
IIUC using to_datetime, the select the position return NaN, then ffill
df.loc[pd.to_datetime(df.Date,errors='coerce').isnull(),'col']=df.Date
df.col=df.col.ffill()
df
Out[867]:
Date Name col
0 EPL A EPL
1 Sunday 21st April 2019 B EPL
2 13:30:00 C EPL
3 13:30:00 D EPL
4 French Ligue 1 E French Ligue 1
5 14:30:00 F French Ligue 1
