Home > Net >  Python: Use the "i" counter in while loop as digit for expressions
Python: Use the "i" counter in while loop as digit for expressions

Time:11-03

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=']))

......

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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)
  • Related