0
for key in CatDict:
    CatName = CatDict[key]['name']
    print('This is the ' + CatName + 'info')
    **Problem Area (CatName + 'DF')** = CalcDF[CalcDF.Cat == CatDict[key]['number']]

Basically what I am trying to do is create a new dataframe that is named by concatenating 'CatName' with the string 'DF' for every iteration of this loop.

I have been having a lot of trouble with that and I am not sure if what I am trying to do is feasible.

The dataframes are already formed outside the for loop I am just doing this to assign them the data that I want

Thank you

6
  • 1
    Please include a minimal reproducible example. In this current state, I have no idea what you are doing. Commented Mar 23, 2021 at 20:14
  • ok I will include more information. Commented Mar 23, 2021 at 20:19
  • I asked this question really badly so I am going to think on it some more and if I can't find a solution reask with a better example. Thanks for the help though Commented Mar 23, 2021 at 20:28
  • 1
    I understand. But think about it this way. Everyone who could possibly answer your question is someone who volunteers their time. So if you want a good answer, you should do your best to make it as easy as possible to answer. For example, you should have provided the code necessary to construct whatever CatDict is. You should also include a representation of what you think the final result should look like. One the more aggrevating things an answerer can encounter is to try to answer someone's question only for the asker to say "that's not what I meant". Commented Mar 23, 2021 at 20:32
  • Yeah I know. I was under the impression that the question I was asking was more or less straight forward but I have been just working on solving this for the last couple of hours so I guess I put too much on the people answering. Commented Mar 23, 2021 at 20:47

2 Answers 2

1

You are trying to create variable variable names. This is not recommended. Rather you could create a dictionary of dataframes with their variable names as keys:

dataframes = {key[‘name’] + 'DF': CalcDF[CalcDF.Cat == CatDict[key]['number']] for key in CatDict}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! this is exactly what I was looking for. Thank you very much
0

You may utilize the built-in function exec as:

for key in CatDict:
CatName = CatDict[key]['name']
print('This is the ' + CatName + 'info')
exec('{}DF = CalcDF[CalcDF.Cat == CatDict[key]['number']]'.format(CatName)) 

1 Comment

Maybe there is a syntax error, but the fundamental idea is to interpret a string as code and execute it

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.