Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add columns to existing dataframe with a loop

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

Your solution is possible if change name of new columns in ouput DataFrame:

df = pd.DataFrame(index=('class1', 'class2', 'class3')) 

r = [df1, df2, df3] 
for f in r: 
    first_col = f.iloc[:,0] 
    df[f.columns[0]] = first_col
print (df)
         A   C   E
class1  10  25  85
class2  45  57  48
class3  59  99  87

Better is use concat with list comprehension for select first column:

r=[df1, df2, df3] 

df = pd.concat([f.iloc[:, 0] for f in r], axis=1)
print (df)
         A   C   E
class1   2  18  62
class2  46  46  11
class3  57  73  92
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading