1

This seems like it should be very simple but am not sure the proper syntax in Python. To streamline my code I want a while loop (or for loop if better) to cycle through 9 datasets and use the counter to call each file out using the counter as a way to call on correct file.

I would like to use the "i" variable within the while loop so that for each file with sequential names I can get the average of 2 arrays, the max-min of this delta, and the max-min of another array.

Example code of what I am trying to do but the avg(i) and calling out temp(i) in loop does not seem proper. Thank you very much for any help and I will continue to look for solutions but am unsure how to best phrase this to search for them.

temp1 = pd.read_excel("/content/113VW.xlsx")
temp2 = pd.read_excel("/content/113W6.xlsx")
..-> temp9

i=1
while i<=9

avg(i) =np.mean(np.array([temp(i)['CC_H='],temp(i)['CC_V=']]),axis=0)             
Delta(i)=(np.max(avg(i)))-(np.min(avg(i)))
deltaT(i)=(np.max(temp(i)['temperature='])-np.min(temp(i)['temperature=']))
i+= 1

EG: The slow method would be repeating code this for each file

avg1 =np.mean(np.array([temp1['CC_H='],temp1['CC_V=']]),axis=0)             
Delta1=(np.max(avg1))-(np.min(avg1))
deltaT1=(np.max(temp1['temperature='])-np.min(temp1['temperature=']))

avg2 =np.mean(np.array([temp2['CC_H='],temp2['CC_V=']]),axis=0)             
Delta2=(np.max(avg2))-(np.min(avg2))
deltaT2=(np.max(temp2['temperature='])-np.min(temp2['temperature=']))

......

  • You need to be using brackets, not parens. Make `temp`, `avg`, `Delta`, and `deltaT` be empty lists, then append to the list instead of doing direct assignments. – Tim Roberts Nov 02 '22 at 22:13
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – G. Anderson Nov 02 '22 at 22:22

3 Answers3

1

Think of things in terms of lists.

temps = []
for name in ('113VW','113W6',...):
    temps.append( pd.read_excel(f"/content/{name}.xlsx") )

avg = []
Delta = []
deltaT = []
for data in temps:
   avg.append(np.mean(np.array([data['CC_H='],data['CC_V=']]),axis=0)             
   Delta.append(np.max(avg[-1]))-(np.min(avg[-1]))
   deltaT.append((np.max(data['temperature='])-np.min(data['temperature=']))

You could just do your computations inside the first loop, if you don't need the dataframes after that point.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • This is very helpful and a different paradigm than I am used to thinking in. Thank you very much for the explanation and think this is going to really help with this and other projects . – Jake Kaiser Nov 02 '22 at 22:40
  • A general rule in Python is, ANY TIME you find yourself creating variables like `xxx1`, `xxx2`, `xxx3` etc., you need to replace that with a list. – Tim Roberts Nov 02 '22 at 23:16
1

The way that I would tackle this problem would be to create a list of filenames, and then iterate through them to do the necessary calculations as per the following:

import pandas as pd

# Place the files to read into this list
files_to_read = ["/content/113VW.xlsx", "/content/113W6.xlsx"]

results = []
for i, filename in enumerate(files_to_read):
    temp = pd.read_excel(filename)
    avg_val =np.mean(np.array([temp(i)['CC_H='],temp['CC_V=']]),axis=0)             
    Delta=(np.max(avg_val))-(np.min(avg_val))
    deltaT=(np.max(temp['temperature='])-np.min(temp['temperature=']))
    results.append({"avg":avg_val, "Delta":Delta, "deltaT":deltaT})

# Create a dataframe to show the results    
df = pd.DataFrame(results)
print(df)

I have included the enumerate feature to grab the index (or i) should you want to access it for anything, or include it in the results. For example, you could change the the results.append line to something like this:

results.append({"index":i, "Filename":filename, "avg":avg_val, "Delta":Delta, "deltaT":deltaT})
ScottC
  • 3,941
  • 1
  • 6
  • 20
  • 2
    This works great and appreciate the response. The enumerate feature and way you iterate through make logical sense from the way I was thinking about this problem. A lot has clicked for this new Python user and thank you for the help – Jake Kaiser Nov 02 '22 at 23:18
  • Hi @JakeKaiser, please consider accepting this response (or one of the others) as the answer. Thanks! – jsmart Nov 02 '22 at 23:36
0

Not sure if I understood the question correctly. But if you want to read the files inside a loop using indexes (i variable), you can create a list to hold the contents of the excel files instead of using 9 different variables.

something like

files = []
files.append(pd.read_excel("/content/113VW.xlsx"))
files.append(pd.read_excel("/content/113W6.xlsx"))
...

then use the index variable to iterate over the list

i=1
while i<=9
    avg(i) = np.mean(np.array([files[i]['CC_H='],files[i]['CC_V=']]),axis=0)             
    ...
    i+=1

P.S.: I am not a Pandas/NumPy expert, so you may have to adapt the code to your needs

Vini
  • 8,299
  • 11
  • 37
  • 49