简体   繁体   English

通过在python中提供条件来创建多个数据框

[英]Creating multiple data frames by providing a condition in python

I'm trying create multiple data frames from a single data-frame.我正在尝试从单个数据帧创建多个数据帧。 I managed to achieve the process by repeating the same line of code, but its looks tedious and is not time friendly.我设法通过重复同一行代码来实现该过程,但它看起来很乏味,而且时间不友好。 I want to create a loop that iterates through the data-frame and create dataframes for me.我想创建一个循环遍历数据帧并为我创建数据帧。 Also I have a list of copy of my Intial dataframe(from which I want to create multiple dataframes).另外我有一个我的初始数据帧的副本列表(我想从中创建多个数据帧)。

df_list = [df_0run,df_1run,df_2run,df_3run,df_4run,df_5run,df_6run]

Please suggest an approach for my question.请为我的问题提出一种方法。

df_0season1=df_0run.loc[df_0run['Season_Id']==1]
df_0season2=df_0run.loc[df_0run['Season_Id']==2]
df_0season3=df_0run.loc[df_0run['Season_Id']==3]
df_0season4=df_0run.loc[df_0run['Season_Id']==4]
df_0season5=df_0run.loc[df_0run['Season_Id']==5]
df_0season6=df_0run.loc[df_0run['Season_Id']==6]
df_0season7=df_0run.loc[df_0run['Season_Id']==7]
df_0season8=df_0run.loc[df_0run['Season_Id']==8]
df_0season9=df_0run.loc[df_0run['Season_Id']==9]

In this fashion I want to create multiple dataframes, and return them as a list.以这种方式,我想创建多个数据框,并将它们作为列表返回。 or multiple lists.或多个列表。

Required output所需的输出

Seasons list = [df_0season1,df_0season2,df_0season3,df_0season4,df_0season5,df_0season6,df_0season7,df_0season8,df_0season9, 
df_1season1,df_1season2,df_1season3,df_1season4,df_1season5,df_1season6,df_1season7,df_1season8,df_1season9, 
df_2season1,df_2season2,df_2season3,df_2season4,df_2season5,df_2season6,df_2season7,df_2season8,df_2season9, 
df_3season1,df_3season2,df_3season3,df_3season4,df_3season5,df_3season6,df_3season7,df_3season8,df_3season9, 
df_4season1,df_4season2,df_4season3,df_4season4,df_4season5,df_4season6,df_4season7,df_4season8,df_4season9,
 df_5season1,df_5season2,df_5season3,df_5season4,df_5season5,df_5season6,df_5season7,df_5season8,df_5season9]

I think you can use a little help of exec over here,我想你可以在这里使用exec的一些帮助,

for i in range (1,10):
    exec("""df_0season{}=df_0run.loc[df_0run['Season_Id']=={}]""".format(i))

This is just a basic application of exec and you can modify it as your requirement.这只是exec 的一个基本应用程序,您可以根据需要对其进行修改。

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

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