Pandas:有没有一种方法可以遍历数据框并使用多个条件创建新的数据框?

Pandas: Is there a way to iterate through a dataframe and create new dataframes using multiple conditions?

我需要拆分一个数据框,以便我可以使用完整数据框内的空间数据创建新文件。

我有一个如下所示的数据框:

Full dataframe

我想使用多个条件创建一些新数据框

我希望最终得到一堆具有条件的数据帧,例如:

Z_year = 2020,Z_month = 5,Z_group = 'weekday',time_range = '08:00 至 12:00'

Z_year = 2020, Z_month = 5, Z_group = 'weekday', time_range = '12:00 至 18:00'

Z_year = 2020, Z_month = 5, Z_group = 'weekday', time_range = '18:00 至 01:00'

Z_year = 2020,Z_month = 5,Z_group = 'weekend',time_range = '08:00 到 12:00'

Z_year = 2020, Z_month = 5, Z_group = 'weekend', time_range = '12:00 至 18:00'

Z_year = 2020, Z_month = 5, Z_group = 'weekend', time_range = '18:00 至 01:00'

Z_year = 2020,Z_month = 8,Z_group = 'weekday',time_range = '08:00 至 12:00'

Z_year = 2020, Z_month = 8, Z_group = 'weekday', time_range = '12:00 至 18:00'

Z_year = 2020, Z_month = 8, Z_group = 'weekday', time_range = '18:00 至 01:00'

...

Z_year = 2021, Z_month = 11, Z_Group = 'weekend', time_range = '18:00 到 01:00'

等等

直到所有组合都完成。

有没有办法遍历我的数据框来做到这一点?

目标列是(年、月、组和时间范围),每个列的唯一值是:(2020, 2021), (5, 8, 11), ('weekday', 'weekend'), 和 (08:00 到 12:00, 12:00 到 18:00, 18:00 到 01:00)

...

#This is used to find unique values for target columns
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')]

...

感谢您的帮助!

您可以像这样遍历数据框:

cols, rows = df.shape
for x in range(cols):
    print(df[x:x+1])

您可以 select 并将候选人附加到列表中并将其转换为新的数据框。

我建议使用 for 循环和字典,以便使用由条件组合组成的谱号保存每个数据帧

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)]