简体   繁体   English

用替换删除系列字符

[英]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? 谁能解释为什么以及如何解决这个问题?

df['datum'].replace(df['datum'][0:2],' ') will replace the first two rows with whitespace, not the first two letters in each row. df['datum'].replace(df['datum'][0:2],' ')将用空格替换前两 ,而不是每行中的前两个字母。 You want to work with df.datum.str 您想使用df.datum.str

Option 1 (If all your months are three letter abbreviations) 选项1 (如果您所有的月份都是三个字母的缩写)
string slicing

df.datum.str[-8:]

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

Option 2 选项2
str.replace with .*\\s(\\w+\\s\\w+)$ str.replace.*\\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

You can using str.split 您可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM