1

When I try to make a dataframe of these two series with differing indices pandas doesn't preserve the order of the columns from the series as shown below.

index = ['one','two','three','four','five','six','seven','eight','nine','ten']
index2 = index[:9] + ['Ha']

a = pd.Series(list(range(10)), index = index)
b = pd.Series(list(range(10)), index = index2)*2

df = pd.DataFrame([a,b], index = ['tens','times2'])

Outputs

Ha  eight   five    four    nine    one     seven   six     ten     three   two

But when I make the dataframe with the series having the same indices the original column order (the order of the list index) is preserved. Why is this happening?

Harshill
  • 21
  • 2
  • Possible duplicate of [How to create a DataFrame while preserving order of the columns?](http://stackoverflow.com/questions/36539396/how-to-create-a-dataframe-while-preserving-order-of-the-columns) – ppasler Jan 19 '17 at 19:34

1 Answers1

1

This is because when the indices of 2 Series don't match Pandas will merge them together and place the columns in alphabetical order. This makes the columns of the DataFrame have the order I guess you expect you need to reorder them after you've made the DataFrame.

df = pd.DataFrame([a, b], index=['tens', 'times2']) df = df.reindex_axis(index + ['Ha'], axis='columns')

df.reindex_axis is faster way of doing df = df[index + ['Ha']]

SARose
  • 3,558
  • 5
  • 39
  • 49