How do I separate the dates in the Date column below. The separating characters are either Y,X or " "?
xx = {'Code': ["001", "002","003"], 'Date': ["202103151716Y202103151716","202103151716X202103151716","202103151716 202103151716"]}
df = pd.DataFrame(data=xx)
df
Code Date
0 001 202103151716Y202103151716
1 002 202103151716X202103151716
2 003 202103151716 202103151716
My solution below finds one of the characters i.e. Y or X at a time but not both of them at the same time. It replaces them with " ", then separates them. Is there any way I can write one statement that looks for either X, Y or space (" ") and separates them using the space? More like an OR statement in my solution below as compared to running the three statements below to replace Y, then replace X.
df['Date'] = df['Date'].str.replace('Y',' ') #Replace Y with space
df['Date'] = df['Date'].str.replace('X',' ')
df[['Date1', 'Date2']] = df['Date'].str.split(' ', 1, expand=True) # Splits the column into two other columns, Date1 and Date2
Expected Output:
Code Date Date1 Date2
0 001 202103151716 202103151716 202103151716 202103151716
1 002 202103151716 202103151716 202103151716 202103151716
2 003 202103151716 202103151716 202103151716 202103151716