我有一个包含日期范围的列,我想分别将其提取到开始日期和结束日期。不确定直接使用datetime.strptime
可以实现
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
谢谢
答案 0 :(得分:3)
使用str.split
例如:
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)
输出:
Index start end
0 1 01 Jan 2019 04 Jan 2019
1 2 07 Jan 2019 11 Jan 2019