繁体   English   中英

Pandas:有没有办法遍历 dataframe 并使用多个条件创建新的数据帧?

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

我需要拆分 dataframe 以便我可以使用完整 dataframe 中的空间数据创建新文件。

我有一个看起来像这样的 dataframe:

全 dataframe

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

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

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 = '周末', time_range = '08:00 to 12:00'

Z_year = 2020, Z_month = 5, Z_group = '周末', time_range = '12:00 to 18:00'

Z_year = 2020, Z_month = 5, Z_group = '周末', 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 = '周末', time_range = '18:00 to 01:00'

等等

直到所有组合完成。

有没有办法遍历我的 dataframe 来做到这一点?

目标列是(年、月、组和时间范围),每个列的唯一值是:(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 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')]

...

谢谢你的帮助!

您可以像这样遍历 dataframe:

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

您可以将 select 和 append 候选列表转换为新的 dataframe。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM