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

BirdBud
  • 143
  • 1
  • 15
  • 1
    Please include a [mcve]. In this current state, I have no idea what you are doing. – piRSquared Mar 23 '21 at 20:14
  • ok I will include more information. – BirdBud Mar 23 '21 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 – BirdBud Mar 23 '21 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". – piRSquared Mar 23 '21 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. – BirdBud Mar 23 '21 at 20:47
  • No worries. it doesn't bother me to move on to the next potential question. I truly am just trying to help you out. I'm glad you got your answer. – piRSquared Mar 23 '21 at 20:53

2 Answers2

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}
RJ Adriaansen
  • 9,131
  • 2
  • 12
  • 26
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))