How do I merge data frames generated in a for loop

Solution for How do I merge data frames generated in a for loop
is Given Below:

I have a loop that creates data frames of similar based on a list as below

Name = ['A','B,'C','D']
for name in Name:
my_dfs[name] = pd.read_csv(r"C:HsOBO%s.csv" % name, delimiter=";")
my_dfs[name].columns = ['Time','s1', 's2','s3','s4','s5','s6']
my_dfs[name]=my_dfs[name].head(10)

The above generate 4 data frames that are similar . I want to merge these data frames into 1 data frame. How do I go about this

This depends on what your expected result is. If you want, you can concatenate the dataframes below each others:

pd.concat(my_dfs,ignore_index=True)

If you really want to ‘merge’ these dataframes, you can do it directly inside the loop:

Name = ['A','B','C','D']
my_df =None
my_dfs ={}
for name in Name:
my_dfs[name] = pd.read_csv(r"C:HsOBO%s.csv" % name, delimiter=";")
my_dfs[name].columns = ['Time','s1', 's2','s3','s4','s5','s6']
my_dfs[name]=my_dfs[name].head(10)
if my_df is None: my_df = my_dfs[name] 
my_df = my_df.merge(my_dfs[name])

in that case, you will need to specify more parameters depending on your case. e.g if its an inner/outer merge or on what columns do you want the merge to happen etc.