pandas 如何合并保存订单? [英] How can a pandas merge preserve order?

查看:75
本文介绍了 pandas 如何合并保存订单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在熊猫中有两个DataFrame,试图将它们合并.但是熊猫一直在改变顺序.我已经尝试设置索引,将它们重置,无论我做什么,我都无法获得返回的输出以相同的顺序排列行.有把戏吗? 请注意,我们从贷款顺序"a,b,c"开始,但合并后的名称为"a,c,b".

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 the returned output to have the rows in the same order. Is there a trick? Note we start out with the loans order 'a,b,c' but after the merge, it's "a,c,b".

import pandas
loans = [  'a',  'b', 'c' ]
states = [  'OR',  'CA', 'OR' ]
x = pandas.DataFrame({ 'loan' : loans, 'state' : states })
y = pandas.DataFrame({ 'state' : [ 'CA', 'OR' ], 'value' : [ 1, 2]})
z = x.merge(y, how='left', on='state')

但是现在订单已不再是原始的'a,b,c'.有任何想法吗?我正在使用熊猫11版.

But now the order is no longer the original 'a,b,c'. Any ideas? I'm using pandas version 11.

推荐答案

希望有人会提供更好的答案,但是如果没有人提供,肯定会起作用,所以……

Hopefully someone will provide a better answer, but in case no one does, this will definitely work, so…

Zeroth,我假设您不想只对loan排序,但要保留任何原始顺序在x中,该顺序可能有也可能没有与loan列的顺序有关. (否则,这个问题会更容易,也不会那么有趣.)

Zeroth, I'm assuming you don't want to just end up sorted on loan, but to preserve whatever original order was in x, which may or may not have anything to do with the order of the loan column. (Otherwise, the problem is easier, and less interesting.)

首先,您要求它根据连接键进行排序.正如文档所述,当您不传递sort参数时,这是默认设置.

First, you're asking it to sort based on the join keys. As the docs explain, that's the default when you don't pass a sort argument.

第二,如果您根据联接键进行排序,则这些行将最终分组在一起,从而使从同一源行合并的两行最终彼此相邻,这意味着您仍然会得到acb.

Second, if you don't sort based on the join keys, the rows will end up grouped together, such that two rows that merged from the same source row end up next to each other, which means you're still going to get a, c, b.

您可以通过以下方式解决此问题:通过将行再次与x合并(在任一侧,这都没关系),或者通过基于重新索引,将行按在原始x中出现的顺序分组在一起.如果愿意,可以在x上.像这样:

You can work around this by getting the rows grouped together in the order they appear in the original x by just merging again with x (on either side, it doesn't really matter), or by reindexing based on x if you prefer. Like this:

x.merge(x.merge(y, how='left', on='state', sort=False))


或者,您可以使用reset_index在其中填充x索引,然后像这样进行排序:


Alternatively, you can cram an x-index in there with reset_index, then just sort on that, like this:

x.reset_index().merge(y, how='left', on='state', sort=False).sort('index')


这两种方法显然都显得有些浪费和笨拙……因此,正如我所说,希望有一个更好的答案,我暂时还没有看到.但如果没有,那行得通.


Either way obviously seems a bit wasteful, and clumsy… so, as I said, hopefully there's a better answer that I'm just not seeing at the moment. But if not, that works.

这篇关于 pandas 如何合并保存订单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆