I try to build a dataframe with the first columns of several other dataframes with a loop. All of them have same index.
df1 = pd.DataFrame(np.random.randint(0,100,size=(3, 2)), columns=list('AB'), index=('class1', 'class2', 'class3'))
df2 = pd.DataFrame(np.random.randint(0,100,size=(3, 2)), columns=list('CD'), index=('class1', 'class2', 'class3'))
df3 = pd.DataFrame(np.random.randint(0,100,size=(3, 2)), columns=list('EF'), index=('class1', 'class2', 'class3'))
df = pd.DataFrame( index=('class1', 'class2', 'class3'))
for f in [df1, df2, df3]:
first_col = f.iloc[:,0]
df[f] = first_col.values
The expected output is a matrix with same formatting as below:
A C E
class1 2 18 62
class2 46 46 11
class3 57 73 92
But this code did not work.
The question mirror this query, but the answers tried (below) did not work. How to add a new column to an existing DataFrame?
df.set_index([first_col], append=True) df.assign(f=first_col.values)