I have 3 lists of data frames and I want to add a suffix to each column according to whether it belongs to a certain list of data frames. its all in order, so the first item in the suffix list should be appended to the columns of data frames in the first list of data frames etc. I am trying here but its adding each item in the suffix list to each column.
In the expected output
- all columns in dfs in cat_a need group1 appended
- all columns in dfs in cat_b need group2 appended
- all columns in dfs in cat_c need group3 appended
data and code are here
df1, df2, df3, df4 = (pd.DataFrame(np.random.randint(0,10,size=(10, 2)), columns=('a', 'b')), pd.DataFrame(np.random.randint(0,10,size=(10, 2)), columns=('c', 'd')),pd.DataFrame(np.random.randint(0,10,size=(10, 2)), columns=('e', 'f')),pd.DataFrame(np.random.randint(0,10,size=(10, 2)), columns=('g', 'h')))cat_a = [df1, df2]
cat_b = [df3, df4, df2]
cat_c = [df1]suffix =['group1', 'group2', 'group3']
dfs = [cat_a, cat_b, cat_c]for x, y in enumerate(dfs):for i in y:suff=suffixi.columns = i.columns + '_' + suff[x]
thanks for taking a look!