How can a pandas merge preserve order?

前端 未结 5 2279
佛祖请我去吃肉
佛祖请我去吃肉 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:37

    Use pd.merge_ordered(), documentation here.

    For your example,

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

    EDIT: Just wanted to point out that default behavior for this function is an outer merge, different from the default behavior of the more common .merge()

提交回复
热议问题