Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

python - Splitting strings with dates of similar cells

I have a dataframe which contains a date column. But the date column is written as second quarter of 2017. I want to change this to 4-1-2017. The problem is the string of the date isn't structured the same for each cell. so what i use now is:

data = [{'date' : '2e kw 2017'},{'date' : '3ekw 2017'},{'date' : '4e kw 2017'} ]

df = pd.DataFrame(data, columns =['date']) 

df[['kwartaal','jaar']] = df.date.str.split(" kw ",expand=True) 

df['kwartaal'] = df['kwartaal'].replace(['2e'],'4-1-')
df['kwartaal'] = df['kwartaal'].replace(['3e'],'7-1-')
df['kwartaal'] = df['kwartaal'].replace(['4e'],'10-1-')

df['date'] = pd.to_datetime(df['kwartaal'] + df['jaar'])

which gives me the desired result for the second and fourth quarter, but not for the third. What is the best way to split this?

        date   kwartaal  jaar
0 2017-04-01       4-1-  2017
1        NaT  3ekw 2017  None
2 2017-10-01      10-1-  2017
question from:https://stackoverflow.com/questions/65920908/splitting-strings-with-dates-of-similar-cells

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let's try replaceing the whole ekw groups with Q and let Pandas handle the conversion:

df['date'] = pd.to_datetime(df['date'].str.replace('es*kws*','Q'))

Output:

        date
0 2017-04-01
1 2017-07-01
2 2017-10-01

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...