0

I have a dataframe index of dates, ranging from 2001 to 2015. I want to isolate the 2015 dates. I want to take advantage of the format -- all dates starting with the full year - e.g. 2004abc...

I would like to simply create a new dataframe, of all elements where the date starts with 2015.

When the column is a string, I am able to do it like this:

df1 = df[df['some column name']=='some string']

But when I try

df[df['some column name']str.startwith('2015')]

I get an error.

Is there anyway to do this?

The error I get is just not recognizing the 'some column name' in df['some column name'].

Edit: Just a short edit to say that I think the problem here was due to trying to filter on the index, which might be handled differently that a regular column. See @Jezrael's answer below which worked.

ZakS
  • 1,073
  • 3
  • 15
  • 27
  • 4
    `df['some column name'].str.startwith('2015')` – BENY May 01 '18 at 19:18
  • 1
    @ZakS, For future readers, can you [edit](https://stackoverflow.com/posts/50122470/edit) with the exact error you get? – jpp May 01 '18 at 19:19
  • @Wen, I am still getting an error. It does not like the column I am filtering on. in my case the column is called "Date" and the error I get is just "Date". – ZakS May 02 '18 at 07:15
  • Hello @COLDSPEED, the question you linked this to is asking how to filter on a column name, but my problem is different. It is asking about the values in the column. Could you open the question please? – ZakS May 02 '18 at 07:19
  • 1
    @ZakS - I think need `df.loc[:, df.columns.str.startswith('2015')]` – jezrael May 02 '18 at 07:29
  • 1
    @ZakS - Another solution is `df.filter(regex='^2015')` – jezrael May 02 '18 at 07:31
  • this works great. – ZakS May 02 '18 at 08:10

0 Answers0