For example I have this code :
path_real = r'C:/Users/SLR CSV'
all_files_real = glob.glob(os.path.join(path , "2*.csv.zip"))
list_real = []
for filename in all_files_real:
df_real = pd.read_csv(filename, index_col=None, header=0, sep=';')
list_real.append(df_real)
So, my list_real is the collection of dataframe.
And, If I want to do a for
loop for each list_real, I will do
for lst in list_real :
lst=datareal
***some code to calculation***
a=[]
*another for loop
b=x/zzz
a.append(b)
Is that possible, in the outer for loop, beside getting lst as each of dataframe, I can iterate for each number of list_real (*I mean len(list_real)).
So, at the end I hope I can get 2d matrix that its column represent a
calculation from each dataframe.
So, I can add :
for i in range (row):
matrix[i][col]=a[i]
My problem is, that col value I do not know how to iterate, lst refer to each df not number of df. collection . Is there a way I can iterate the df it self and also, number of df so I can put this number on my matrix(i)(*desired variable)?
CodePudding user response:
To achieve what you described, you can use the enumerate function to loop through the list_real and get the index of each element (dataframe) as well as the element itself. This way, you can use the index to keep track of the column number in the matrix.
Here is how you can modify your code to achieve this:
# Create an empty matrix with the desired number of rows
matrix = [[0 for j in range(len(list_real))] for i in range(len(a))]
# Loop through the list_real and get the index and element (dataframe)
for col, lst in enumerate(list_real):
lst = datareal
# Some code to calculation
a = []
# Another for loop
for i in range(len(lst)):
b = lst[i] / zzz
a.append(b)
# Fill the matrix with the values in a
for i in range(len(a)):
matrix[i][col] = a[i]
This will create a matrix with the desired number of rows and columns, and fill it with the values calculated from each dataframe in list_real. The matrix will have as many columns as there are elements in list_real, and the values in each column will correspond to the calculation from the corresponding dataframe.