0

Hihi, I'm reasonably new to Python, more a R guy. But I'm required to use python for a task.

However, I encountered a situation where I need to subset data into different dataFrame by the date. "in future not 3 exactly, therefore want to create a loop to do this" I want to potentially create 3 dataframes like

df_train_30 which contains start day  0 - 30
df_train_60 which contains start day 30 - 60
df_train_90 which contains start day 60 - 90

but not sure how to achieve this... pls help.

idea in code below

today = pd.to_datetime('now')
df_train['START_DATE'] = pd.to_datetime(df_train['START_DATE'])
previous_day_del = 0

for day_del in (30,60,90):
    **'df_train_' + str(day_del)** = df_train[(df_train['START_DATE']>= today - timedelta(days=day_del)) & (df_train['START_DATE']< today - timedelta(days=previous_day_del))]
    previous_day_del = day_del
2
  • 1
    kindly post your expected output. I'm guessing it has to be as a dataframe? Commented Sep 2, 2020 at 5:53
  • yes, my expected output is having 3 different data frames like 1) df_train_30: contains results of START_DATE between SYSDATE - 30 to SYSDATE, 2) df_train_60: contains results of START_DATE between SYSDATE - 60 to SYSDATE -30, 3) df_train_90: contains results of START_DATE between SYSDATE - 90 to SYSDATE -60 Commented Sep 2, 2020 at 5:57

1 Answer 1

1

You could probably store it into a dictionary - it's easier to manage rather than dynamically generated variables. A dictionary's more of an object with key-value pairs, and the values can be of almost any type, including even dataframes. Here's a quick guide on Python dictionaries that you could look at.

In your example, you could probably go ahead with this:

today = pd.to_datetime('now')
df_train['START_DATE'] = pd.to_datetime(df_train['START_DATE'])
previous_day_del = 0
   
# Creating an empty dictionary here called dict_train
dict_train = {}

for day_del in (30,60,90):
    dict_train[day_del] = df_train[(df_train['START_DATE']>= today -timedelta(days=day_del)) & (df_train['START_DATE']< today - timedelta(days=previous_day_del))]
    previous_day_del = day_del

Hope this helps, cheers! :)

Sign up to request clarification or add additional context in comments.

1 Comment

thousand thanks! Yes, exactly what I needed. thanks for the dictionaries link. was wondering how to turn dictionary back to df but seems found in there.

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.