-1

I have a single column dataframe 'new_list' like so:

   list    
0   LAX
1   CHI
2   HOU

that I am concatting to my 'global_list' dataframe which looks like this:

   list  src dest
0   LAX    1    0
1   CHI    0    1

to hopefully add 'HOU' as a row to 'global_list' while maintaining order like so:

   list  src dest
0   LAX    1    0
1   CHI    0    1
2   HOU   NaN NaN

However, when I run global_list = pd.concat([global_list, new_list]).groupby('list', as_index=False).first()

I end up with the src and dest columns flipped (I assume because they are getting sorted alphabetically)

   list  dest src
0   LAX    0    1
1   CHI    1    0
2   HOU   NaN NaN

I read up you can set a sort=False in the concat statement, but that solution is for a newer version of pandas (I am on 0.20.1 and can't update). Is there another way to maintain order when concatting for this older version of pandas?

thePandasFriend
  • 115
  • 2
  • 10
  • 2
    I can't really test with this specific version but as a workaround, you can just reorder with a list of column of the original df, before you do `cols = global_list.columns` and then when you do the concatenation, you can use it with `global_list = pd.co...first()[cols]` or something like it – Ben.T Jul 01 '20 at 17:57
  • yup I just reordered manually – thePandasFriend Jul 01 '20 at 18:14

1 Answers1

1

Before you concat do

cols = global_list.columns.tolist()

and then after you concat you can reorder like:

global_list = global_list[cols]

this is a workaround if you cannot upgrade pandas to it's latest supported version.

julian
  • 451
  • 2
  • 8