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

Pandas – Adding Column to DataFrame, but Items Get Reordered

I’m trying to simply add a column from another dataframe of the same length using df[‘columnname’] = df2[‘columnname’], but the column appears in df with the lines in a different order.

Is there any rhyme or reason to this? Am I doing pandas wrong? Would love any help for this low level data transformation.

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

>Solution :

Problem is different indices in both Dataframes, you can assign array or list for avoid it, only necessary same length of both DataFrames:

df = pd.DataFrame({'col': [0,1,2]})
df2 = pd.DataFrame({'columnname': [5,6,7]}, index=[1,0,2])

df['columnname'] = df2['columnname']
df['columnname1'] = df2['columnname'].to_numpy()
df['columnname2'] = df2['columnname'].to_list()

print (df)
   col  columnname  columnname1  columnname2
0    0           6            5            5
1    1           5            6            6
2    2           7            7            7
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