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.
CodePudding user response:
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
CodePudding user response:
You can use reindex
to avoid different indexes:
df['columnname'] = df2.reindex(df.index)['columnname']