I am trying to parse the date and time from a string column.
This is the original column (all one column):
description
4/18/2020 21:05 XXXXXXXXXXXXXXXXXXXXXXXXXXX YYYYYYYYYYYY ZZZZZZZZZZZZZZZZZ
my desired output:
spliced_date spliced_time
4/18/2020 21:05
I am looking to pick out the date and time into their own seperate columns.
CodePudding user response:
You could use str.split
:
df = pd.DataFrame({'description': ['4/18/2020 21:05 XXXXXXXXXXXXXXXXXXXXXXXXXXX YYYYYYYYYYYY ZZZZZZZZZZZZZZZZZ']})
df[['Date','Time']] = df['description'].str.split(n=2, expand=True)[[0,1]]
df = df.drop(columns='description')
Output:
Date Time
0 4/18/2020 21:05
CodePudding user response:
You can use named capture group with str.extract
:
pattern = r'(?P<spliced_date>[^\s] )\s (?P<spliced_time>[^\s] )'
df = df['description'].str.extract(pattern)
print(df)
# Output:
spliced_date spliced_time
0 4/18/2020 21:05