Khaine775
Khaine775

Reputation: 2765

Pandas extracting substring

I have a column containing dates, which could look like 2017-10-12. I want to create a new column containing the day, which in my case would be the number between the two -'s. I've tried various .str.extract() queries, but I can't seem to get it right.

df['days'] = df['dates'].str.extract('(-*)')

Any hints?

Upvotes: 1

Views: 64

Answers (1)

jezrael
jezrael

Reputation: 862406

Use split and select second list by str[1]:

df['days'] = df['dates'].str.split('-').str[1]

Or to_datetime with format parameter + dt.day:

df['days'] = pd.to_datetime(df['dates'], format='%Y-%d-%m').dt.day

Upvotes: 4

Related Questions