Using a for loop with two variables in Python

101 Views Asked by At

I want to use two variables in a loop. Currently, I am running into an error using the following code; The filenames generally contain four column (time, power, time, power) with a lot of rows (up to 1000).

filename = [filename1, filename2, filename3, filename4, filename5]
n = len(filename)
for i, j in range(n):
    df = pd.read_csv(filename[i], delim_whitespace=True,skiprows=4, encoding='latin1') # [0] = filename1
    Time = df[j].iloc[:,0]
    Power = df[j].iloc[:,1]
    Time1 = df[j].iloc[:,2]
    Power1 = df[j].iloc[:,3]
    print("power", Time1)

There are multiple filenames (currently 5 but I might use more, hence "n". The filenames should be implemented in the df = row. Then, below I want to extract separate columns from each file, therefore I want to use the "Time = df[j].iloc[:,0] lines. However, this does not work:

TypeError: cannot unpack non-iterable int object

How can I adjust the code so that it works?

2

There are 2 best solutions below

1
dkasa On BEST ANSWER

The for loop just unpacks the values you give it. So in this case range(n) only returns a single int. If you want to unpack multiple values you need to give it something else like a zip/enumerate builtin function which creates tuples.

To fix this error you need to remove the i, from the for loop.

Or skip getting the length of the list and use for i, df in enumerate(filename).

2
Priyas Paulzagade On

The issue in your code is with the for loop. The range(n) function returns an iterable, but the for loop is trying to unpack the values into two variables (i and j). The correct way to iterate over the indices of the filename list would be to use enumerate. Also, it seems like you want to iterate over each column of each DataFrame. Here's how you can adjust your code:

import pandas as pd

filename = [filename1, filename2, filename3, filename4, filename5]
n = len(filename)

for i, file in enumerate(filename):
    df = pd.read_csv(file, delim_whitespace=True, skiprows=4, encoding='latin1')
    
    # Assuming you want to iterate over each column of the DataFrame
    for j in range(df.shape[1]):
        Time = df.iloc[:, 2*j]  # assuming time columns are even-numbered indices (0-based)
        Power = df.iloc[:, 2*j + 1]  # assuming power columns are odd-numbered indices (0-based)
        print(f"Power in file {i}, column {j}:", Power)

This code uses enumerate to iterate over both the index i and the filename file. Inside the loop, it iterates over each column index j of the DataFrame and extracts the Time and Power columns accordingly.

Make sure to adjust the column indexing based on your actual data structure. If the columns are not strictly in pairs, you may need a different approach for indexing the columns.