Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

python 3.x - Extract Datetime information from a string in a DataFrame column

So I have the Edition Column which contains data in unevenly pattern, as some have ',' followed by the date and some have ',-' pattern.

df.head()

17                Paperback,– 1 Nov 2016
18    Mass Market Paperback,– 1 Jan 1991
19                      Paperback,– 2016
20               Hardcover,– 24 Nov 2018
21        Paperback,– Import, 4 Oct 2018

How can I extract the date to a separate column. I tried using str.split() but can't find specific pattern to extract.Is there any method I could do it?


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

1 Answer

0 votes
by (71.8m points)
obj = df['Edition']
obj.str.split('((?:d+s+w+s+)?d{4}$)', expand=True)

or

obj.str.split('[,–]+').str[0]
obj.str.split('[,–]+').str[-1] # date

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...