0

I am trying to create a for loop wherein a new dataframe is created based on a conditional statement of a column, but all I end up with is are only the column names without any values.

What I am doing now is the following:

for i in range(3):
    df_new = df[df['col'] == i]

but this does not seem to work.

1 Answer 1

1

Essentially with this for loop you are overwriting df and since the first and second conditions can't be applied simultaneously, you will end with an empty dataframe.

You could create them dynamically within a dictionary, where the key is the name of the dataframe and the value the filtered dataframe. Try with:

dfs = {}
for i in range(3):
   dfs["df_"+str(i)] = df[df['col'] == i]

This will generate a dictionary that looks like the following structure:

{df_1:df[df['col']==1],
 df_2:df[df['col']==2]}

And you can access them via its key name dfs['df_1']

Sign up to request clarification or add additional context in comments.

7 Comments

Ah wait, that is a mistake from my part. I am creating the new df from another one. I will edit my original post. It just doesn't seem to iterate over i.
After your reviewing the edit, the same issue applies, the variable is being overwritten after each iteration.
if I do it for just 1 (range(1)) iteration it also does not do anything. It's fine if it gets overwritten, its for a larger function, but it just doesnt do anything.
The slicing is correct, are you sure col is of numeric type and not string or object? '1' != 1
Yeah, if I just type df[df['col'] == 1] then I get the full df as required
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.