I am creating multiple dataframes numbered from 1 to n through a loop. The first thing I do for this, is create a dictionary:
dict_of_df = {}
then runs the loop:
for i in range(1, n 1):
... import data
... manipulate data
... create a dataframe corresponding to the "i" value of the loop
dict_of_df["df_{}".format(i)] = _my_last_manipulated_data_
This creates a dictionary of dataframes named df_1,df_2...df_n, and I can access each of them outside the loop for further treatment using:
df_1 = dict_of_df["df_1"]
df_2 = dict_of_df["df_2"]
...
etc
The problem is that the data I import are very similar, and all the dataframes resulting from the loop, df_1, df_2, ... df_n, have the same number of columns sharing the same names; that is to say, all the df_i have the 4 columns names A,B,C,D.
What could I do inside my loop in order to have names A_1,B_1,C_1,D_1 when df_1 is created, then A_2,B_2,C_2,D_2 when df_2 is created, and so on...?
I guess the last line of the loop, dict_of_df["df_{}".format(i)] = ... should be modified, but I don't see in what way. I thought to introduce enumerate somewhere, but I don't know in what form exactly.
CodePudding user response:
IIUC, you could use add_suffix method. So use the line below in the last line of your loop:
dict_of_df["df_{}".format(i)] = _my_last_manipulated_data_.add_suffix(f'_{i}')
