Python & Pandas: How can I create new smaller data frames from my large data frame in a (for) loop?

Viewed 27

I have a large data frame (51 rows x 51 columns) where the first column are the X-values and the other 50 columns are Y-values of 50 mathematical functions.

Something like this:

x   y1   y2   y3 ... y49  y50
0   1    0.5  20     200  0
1   2    0.1  5      199  0
2   3   -0.2  10     198  1
3   4   -1    15     197  1
...               
50  51  -70   65     151  15

I want to make a list of 50 smaller data frames where each df list element has the same x-values and a certain y-value column.

So the result basically should look like this:

df1    
x   y1 
0   1    
1   2    
2   3     
3   4     
...               
50  51  

df2
x    y2   
0    0.5  
1    0.1  
2   -0.2 
3   -1   
...               
50  -70   
etc.

I didn't find any clues how to do this in an automated way with a for loop. Any ideas how? Thanks in advance!

1 Answers

You can use a list comprehension to generate the columns to retain for each smaller DataFrame:

[df[["x", f"y{i + 1}"]] for i in range(50)]

The first two elements are:

   x  y1
0  0   1
1  1   2
2  2   3
3  3   4

and

   x   y2
0  0  0.5
1  1  0.1
2  2 -0.2
3  3 -1.0
Related