当我尝试使用不同的索引制作这两个系列的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
的顺序)。为什么会这样?
答案 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']]