Ken Neth
Ken Neth

Reputation: 21

remove character in series with replace

I have a series that looks like following:

datum

02-jun-18

01-jun-18

01-jun-18

30-maj-18

30-maj-18

29-maj-18

27-maj-18

25-maj-18

25-maj-18

25-maj-18

14-maj-18

I want to remove the days on each row but keep the month and year with the follwing code:

df['datum']=df['datum'].replace(df['datum'][0:2],' ') 

But it does not work. Can anyone explain why and how I can tackle this problem?

Upvotes: 0

Views: 147

Answers (2)

BENY
BENY

Reputation: 323316

You can using str.split

pd.Series(['18 may 2018','10 jun 2018']).str.split(' ',1).str[1]
Out[209]: 
0    may 2018
1    jun 2018
dtype: object

Upvotes: 0

user3483203
user3483203

Reputation: 51165

df['datum'].replace(df['datum'][0:2],' ') will replace the first two rows with whitespace, not the first two letters in each row. You want to work with df.datum.str

Option 1 (If all your months are three letter abbreviations)
string slicing

df.datum.str[-8:]

0    jun 2018
1    jun 2018
2    maj 2018
Name: datum, dtype: object

Option 2
str.replace with .*\s(\w+\s\w+)$

df.datum.str.replace(r'.*\s(\w+\s\w+)$', r'\1')

0    jun 2018
1    jun 2018
2    maj 2018
Name: datum, dtype: object

Upvotes: 1

Related Questions