How can a pandas merge preserve order?

前端 未结 5 2287
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 03:46

I have two DataFrames in pandas, trying to merge them. But pandas keeps changing the order. I\'ve tried setting indexes, resetting them, no matter what I do, I can\'t get th

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 04:13

    Pandas has a merge_ordered function, so your solution is now as simple as:

    z = pd.merge_ordered(x, y, on='state')
    

提交回复
热议问题