基本问题-我正在尝试连接两个DataFrame,结果DataFrame保留原始两个索引的顺序。例如:
df = pd.DataFrame({'Houses':[10,20,30,40,50], 'Cities':[3,4,7,6,1]}, index = [1,2,4,6,8])
df2 = pd.DataFrame({'Houses':[15,25,35,45,55], 'Cities':[1,8,11,14,4]}, index = [0,3,5,7,9])
使用pd.concat([df, df2])
简单地追加到DF2 DF1结束。我试图将它们连接起来以产生正确的索引顺序(0到9)。
concat
与参数sort
一起使用以避免警告,然后DataFrame.sort_index
:
df = pd.concat([df, df2], sort=False).sort_index()
print(df)
Cities Houses
0 1 15
1 3 10
2 4 20
3 8 25
4 7 30
5 11 35
6 6 40
7 14 45
8 1 50
9 4 55
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句