1

I have a column that contains a date range and I'd just like to have it extracted to the start and end dates respectively. Not sure if this is doable with datetime.strptime directly

df_have = pd.DataFrame([[1, '01 Jan 2019-04 Jan 2019'], [2, '07 Jan 2019-11 Jan 2019']], columns=['Index', 'Range']) Index Range 0 1 01 Jan 2019-04 Jan 2019 1 2 07 Jan 2019-11 Jan 2019 df_want = pd.DataFrame([[1, '01 Jan 2019', '04 Jan 2019'], [2, '07 Jan 2019', '11 Jan 2019']], columns=['Index', 'Start', 'End']) Index Start End 0 1 01 Jan 2019 04 Jan 2019 1 2 07 Jan 2019 11 Jan 2019 

Thanks

0

2 Answers 2

3

Use str.split

Ex:

import pandas as pd df_have = pd.DataFrame([[1, '01 Jan 2019-04 Jan 2019'], [2, '07 Jan 2019-11 Jan 2019']], columns=['Index', 'Range']) df_have[["start", "end"]] = df_have.pop("Range").str.split("-", expand=True) #Thanks @ jezrael print(df_have) 

Output:

 Index start end 0 1 01 Jan 2019 04 Jan 2019 1 2 07 Jan 2019 11 Jan 2019 
Sign up to request clarification or add additional context in comments.

1 Comment

I see now that I've thought about the q from a datetime perspective whereas it could easily be solved using str.split. Thanks for the answer though
0
import pandas as pd df_have['Start']=df_have['Range'].map(lambda x:x.split('-')[0]) df_have['End']=df_have['Range'].map(lambda x:x.split('-')[1]) df_have.drop('Range', axis=1) print(df_have) Index Start End 0 1 01 Jan 2019 04 Jan 2019 1 2 07 Jan 2019 11 Jan 2019 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.