2

I have some data and I want to separate it based on values. For example when x and y are below 100 to keep these values in a separate dataframe when x is below 200 and y below 100 to keep these separate. Essentially I want to create 2 loops, one inside another, and assign a name something like df+i+j. However, I get an error when I try to do it. Here is the code I have developed but does not function. Thank you for your help in advance!

x1=100
y1=100
for i in range(9):
    for j in range(9):
        dframe+str(i)+str(j) = df[(df['x'] <= x1) & (df['y'] <= y1)]
        y1 = y1+100
    x1 = x1+100
Vasilis
  • 143
  • 10
  • 5
    if you are using `pandas`, avoid using loops and look for `pandas` functions. And to be more clear add sample data and expected output – Sociopath Nov 14 '19 at 11:20
  • 1
    You can't assign variable in variable name. The best way to store and access your different dataframes is to put them in an array – Clément Nov 14 '19 at 11:20
  • 1
    Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – roganjosh Nov 14 '19 at 11:22
  • I would help to know what you want to do after this. – Alex Hall Nov 14 '19 at 12:22
  • Essentially I have two variables x and y. Both have a range from 0 to 2000. After a separate them into groups of 0 to 100 then 100 to 200 etc. I want to keep only 70% of the observations for each group. I do this as I have a big dataset and if I remove data randomly it changes the distribution a lot. So I want to break it into groups and then remove a part of the observations per group. – Vasilis Nov 14 '19 at 12:41

1 Answers1

0
x1 = 100
y1 = 100
dframes = dict()
for i in range(9) :
    for j in range(9) :
        dframes['dframe' + str(i) + str(j)] = df[(df['x'] <= x1) & (df['y'] <= y1)]
        y1 += 100
    x1 += 100

try this