I have a list of dynamically named 2-dimensional dataframes, and I would like to loop through the list, subtracting a value from all values within each dataframe.
The dataframes are created with random numbers, and the dimensions as well as the number of dataframes is determined by global parameters at the top of the code.
For instance, taking a very ‘simple’ example (i.e., keeping the numbers artificially small for the sake of brevity):
pop_size=3
x_dim=2
y_dim=2
sub_val=4
batch_list = [ ]
for i in range(pop_size):
bt=pd.DataFrame(np.random.rand(x_dim, y_dim))
batch_list=append(pd.DataFrame((bt == bt.max(axis=0)).astype(int)))
print(f’BATCH[{i}]:’)
print(batch_list[i])
output:
BATCH[0]:
0 1
0 1 0
1 0 1
BATCH[1]:
0 1
0 1 1
1 0 0
BATCH[2]:
0 1
0 0 1
1 1 0
From this initial group, I need to create a new list of dataframes made up of the above and a subtracted value (sub_val).
With the parameter set to 4 (sub_val=4), the desired outcome should be:
new_list[0]:
0 1
0 3 4
1 4 3
new_list[1]:
0 1
0 3 3
1 4 4
new_list[2]:
0 1
0 4 3
1 3 4
I have tried many things, including using the 'sub' function with Pandas, as well as ‘subtract’ with Numpy. And for some reason I always get errors, or just a one dimensional list of my ‘list indices’. So, somewhere in the referencing of the non-discreetly named dataframes, my counter is not being resolved back to the specific dataframe it represents, or my dataframe is processed as a ‘list object’ instead of the two-dimensional matrix it is supposed to be.
Note, the list of dataframes must be dynamically referenced, as the parameters of dimension, population size, and ‘subtracted value’ change depending on the application. These numbers can be quite large. I have kept them small here for the sake of simplicity.
Here is an example of just one of many of my attempts (this one using Numpy ‘subtract’):
new_list = []
for p in batch_list:
new_df = [pd.DataFrame(batch_list[p])]
out=[np.subtract(sub_val, new_df)]
new_list.append(out)
print(new_list)
From which I get the following error: **TypeError: ** list indices must be integers or slices, not list
Any advice you can give, I will be greatly appreciative – Thank you.