Split the dataframe with a minimal way in pandas

Question:

I have 10 different dataframe with shape 1000*1000. I have to use a part of this data frames for the training and validation. Now I am using the following lines of code to separate this dfs:

 df1_train = df1.loc[:, Start:end]
 df2_train = df2.loc[:,Start:end]
 df3_train = df3.loc[:,Start:end] 
 ...

And this make a lot of lines of codes. I am looking for a more minimal way to do this. Do you know any option in the pandas or numpy for this? Thank you

Asked By: Sadcow

||

Answers:

orig_dfs = [df1, ..., dfn]
train_dfs = []
for df in orig_dfs:
        train_dfs.append(df.loc[:, start:end])
Answered By: jtb

IIUC, use globals :

list_dfs = ["df1", "df2", .. ]

for df in list_dfs:
    globals()[f"{df}_train"] = eval(df).loc[:, Start:end]

Or, if the dataframes names have the same pattern (which is the case here), use this :

N = 10 # <- number of dataframes

for i in range(N):
    df_name = f"df{i+1}"
    globals()[f"{df_name}_train"] = eval(df_name).loc[:, Start:end]
Answered By: Timeless
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.