1

I am trying to create 10 new dataframes from one larger dataframe based on some criteria. See code below.

for i in range(1,11):
    'x_'+str(i) = CRSP_mom[(CRSP_mom['mom_rank'] > (float(i)-1.0)/10)]

I get the error.

  File "<ipython-input-167-902910fdab60>", line 2
    'x_'+str(i) = CRSP_mom[(CRSP_mom['mom_rank'] > (float(i)-1.0)/10) & (CRSP_mom['mom_rank'] <= (float(i))/10)]
SyntaxError: can't assign to operator

Any thoughts on how I could get it to create a dataframe with the looped name?

Thanks much all.

1 Answer 1

3

In general, dynamic variable creation is not a good idea.

How about a dictionary?

d = {'x' + str(i) : CRSP_mom[(CRSP_mom['mom_rank'] > (float(i)-1.0)/10) & (CRSP_mom['mom_rank'] <= (float(i))/10)] for i in range(1, 11)}
Sign up to request clarification or add additional context in comments.

1 Comment

Useful. Worked perfectly. Thanks.

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.