0

Currently I am cutting my data in what I assume is an inefficient manner. I want to know if it is possible to use conditional formatting via a datetime without creating and dropping a column.

I can solve my problem, but currently I am using

input_df['YEAR'] = pd.DatetimeIndex(input_df["TERMSTARTDATE_FORMATTED"]).year

then

recent_df = input_df.drop(input_df[input_df.YEAR < 2018].index)

This works but I can see that it's not elegant. I think there should be a way to just drop the columns from the dataframe without first creating a new column.

Is there a more elegant solution to my problem? What is the pythonic way of dealing with this?

Violatic
  • 374
  • 2
  • 18
  • Use `recent_df = input_df[input_df.YEAR >= 2018]` – jezrael Feb 05 '19 at 14:32
  • But that still requires me to make a .YEAR column. Whereas I don't want to create that first. I want to use "TERMSTARTDATE_FORMATTED" which is a datetime object – Violatic Feb 05 '19 at 14:35
  • 1
    Yes, then use `recent_df = input_df[input_df["TERMSTARTDATE_FORMATTED"].dt.year >= 2018]` – jezrael Feb 05 '19 at 14:35

0 Answers0