2

Background

I have a dataframe df. It has order_dates for customers(when was transaction done etc. I am trying to create a new column recent which will have 1 if the transaction was recent(within 1 month from today) and 0 if it is not a recent transaction.

e.g.

Current df

 order_date
 2019-01-01
 2019-12-14
 2019-10-12
 2019-11-22

Output df needed

 order_date   recent
 2019-01-01    0
 2019-12-14    1
 2019-10-12    0
 2019-11-22    0
 2019-12-09    1

Code:

   import datetime
   df['recent'] = 9
   df['order_date'] = pd.to_datetime(df['order_date']).dt.date

   if ((df['order_date']) >(datetime.date.today() - datetime.timedelta(1*365/12))).any():
    df['recent'] == 1
   else:
    df['recent'] == 0

I wrote default value 9 for recent because if else was not working and now I am getting all 9 values in df['recent']

Some basic background:

[IN]:print(df['order_date'].loc[0])
2019-01-01
[IN]:type(df['order_date'])
pandas.core.series.Series
[IN]:print(datetime.date.today() - datetime.timedelta(1*365/12))
2019-12-01
[IN]:type(datetime.date.today() - datetime.timedelta(1*365/12))
datetime.date
2
  • how can row 2 be 1 when the transaction was in october? it was more than 50 days ago only row 1 should be a 1 Commented Dec 31, 2019 at 12:22
  • corrected @Datanovice Commented Dec 31, 2019 at 14:47

1 Answer 1

2

Convert boolean mask to integers by Series.astype:

df['order_date'] = pd.to_datetime(df['order_date']).dt.date
#solution for oldier pandas versions
#df['order_date'] = pd.to_datetime(df['order_date']).dt.floor('d')


m = (df['order_date']) >(datetime.date.today() - datetime.timedelta(1*365/12))

df['recent'] = m.astype(int)

Or set values by numpy.where:

df['recent'] = np.where(m, 1, 0)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi this works. Thanks. But how to make it work with if else.
@ShailajaGuptaKapoor - If use any() like in question it means if at least one True in mask m set df['recent'] to1 - all values, else set all values to 0. So if want set correct way in pandas values by condition use numpy.where, it is numpy/pandas alternative for if-else statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.