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
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: :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.