从系列制作DataFrame时保持列顺序

时间:2017-01-19 19:32:41

标签: python python-3.x pandas

当我尝试使用不同的索引制作这两个系列的dataframe时,pandas不会保留系列中列的顺序,如下所示。

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'])

输出

Ha  eight   five    four    nine    one     seven   six     ten     three   two

但是当我使用具有相同索引的系列dataframe时,保留原始列顺序(列表index的顺序)。为什么会这样?

1 个答案:

答案 0 :(得分:1)

这是因为当2 Series的索引不匹配时,Pandas会将它们合并在一起并按字母顺序放置列。这使得DataFrame的列具有我猜您在生成DataFrame后需要重新排序的顺序。

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

df.reindex_axis是执行df = df[index + ['Ha']]

的更快捷方式