DD DD
DD DD

Reputation: 1238

How to split date by Python

I have the date like below.

7/1 09:00
7/1 12:00
7/1/19 12:00
7/2/19 13:00
7/3 4:00*

And I want to split this date only to get date without the time and year.

7/1 
7/1 
7/1
7/2
7/3 

I m thinking of using lambda function. But I'm not sure how to use this.

order['orderDate'] = train.date.apply(lambda x : x.split).astype('int')

Thank you so much for reading.

Upvotes: 1

Views: 162

Answers (4)

NGeorgescu
NGeorgescu

Reputation: 27

Depending on your input constraints, if your input is plaintext, consider the regex re.findall(r'^\d+/\d+', text, flags=re.MULTILINE) which looks for "digits, a slash, and then more digits, at the start of a line". The flags is there to tell your regex engine that ^ is the start of every line, not the start of the whole text.

Upvotes: 0

shaik moeed
shaik moeed

Reputation: 5785

You can use lambda like this,

order['orderDate'] = train.date.apply(lambda x : '/'.join(x.split('/')[:2]))

Upvotes: 0

awakenedhaki
awakenedhaki

Reputation: 301

train.date.str.extract(pat=r'^(\d+/\d+).*').astype(int)

Upvotes: 0

kederrac
kederrac

Reputation: 17322

you can use:

 train.date.str.extract(r'(\d+/\d+)')

Upvotes: 1

Related Questions