1

I have a massive block of code that creates the following arrays (Var1 ... Var100). I was wondering if there is way to actually write a python script to create and execute the code within a for loop. However, I've been unsuccessful so far in doing so.

Var1 = np.where((df['Q_1'] * df['CapR1'])>=(1*df['CapR1']),df['CapR1'],0)
Var2 = np.where((df['Q_2'] * df['CapR2'])>=(1*df['CapR2']),df['CapR2'],0)
...
Var100 = np.where((df['Q_100'] * df['CapR100'])>=(1*df['CapR100']),df['CapR100'],0)

I've tried the following but doesn't work:

for i in range(1,101):
    'Var' + str(i) = np.where((df['Q_' + str(i)] * df['CapR' + str(i)])>=(1*df['CapR' + str(i)]),df['CapR' + str(i)],0)
prks21
  • 11
  • 2
  • 2
    why not store your results in a list? then you can do the whole thing in a list comprehension or a `for` loop. – i alarmed alien Jul 27 '18 at 11:28
  • 1
    Agree with @ialarmedalien. The linked answers have interesting magic, but a list is better... –  Jul 27 '18 at 11:29
  • See this link https://stackoverflow.com/questions/8028708/dynamically-set-local-variable It should be helpful – dmeleshko Jul 27 '18 at 11:30

0 Answers0