I need to split a dataframe up so that I can create new files using the spatial data inside the full dataframe.
I have a dataframe that looks like this:
I'd like to create some new dataframes using multiple conditions
I'm hoping to end up with a bunch of dataframes with conditions where, for example:
Z_year = 2020, Z_month = 5, Z_group = 'weekday', time_range = '08:00 to 12:00'
Z_year = 2020, Z_month = 5, Z_group = 'weekday', time_range = '12:00 to 18:00'
Z_year = 2020, Z_month = 5, Z_group = 'weekday', time_range = '18:00 to 01:00'
Z_year = 2020, Z_month = 5, Z_group = 'weekend', time_range = '08:00 to 12:00'
Z_year = 2020, Z_month = 5, Z_group = 'weekend', time_range = '12:00 to 18:00'
Z_year = 2020, Z_month = 5, Z_group = 'weekend', time_range = '18:00 to 01:00'
Z_year = 2020, Z_month = 8, Z_group = 'weekday', time_range = '08:00 to 12:00'
Z_year = 2020, Z_month = 8, Z_group = 'weekday', time_range = '12:00 to 18:00'
Z_year = 2020, Z_month = 8, Z_group = 'weekday', time_range = '18:00 to 01:00'
...
Z_year = 2021, Z_month = 11, Z_Group = 'weekend', time_range = '18:00 to 01:00'
etc.
Until all combinations are complete.
Is there a way to iterate through my dataframe to do this?
The target collumns are (Year, month, group, and time range), and the unique values for each are: (2020, 2021), (5, 8, 11), ('weekday', 'weekend'), and (08:00 to 12:00, 12:00 to 18:00, 18:00 to 01:00)
...
#This is used to find unique values for target collumns
year_list = df['Z_year'].unique()
#Creating a new dataframe using only target values
a = df.loc[(df['Z_year'] == 2020) & (df['Z_month'] == 5) & (df.loc['Z_group'] == 'weekday') & (df.loc['time_range'] == '08:00 to 12:00')]
...
Thanks for your help!
CodePudding user response:
You can iterate through the dataframe like this:
cols, rows = df.shape
for x in range(cols):
print(df[x:x 1])
You can select and append candidates into a list and convert it into a new dataframe.
CodePudding user response:
I suggest to use a for loop and a dictionary so to save each dataframe with a clef composed of the combination of the conditions
dic = {}
for a in [2020,2021]:
for b in [5, 8, 11]:
for c in ['weekday', 'weekend']:
for d in ['08:00 to 12:00', '12:00 to 18:00', '18:00 to 01:00']:
idf = str(a) str(b) c d
dic[idf] = df.loc[(df['Z_year'] == a) & (df['Z_month'] == b) & (df.loc['Z_group'] == c) & (df.loc['time_range'] == d)]