假设我有两个数据框x
and y
在 Pandas 中,我想填写一列x
对列进行排序的结果y
。我试过这个:
x['foo'] = y['bar'].order(ascending=False)
但这没有用,我suspect因为熊猫对齐索引之间x
and y
(具有相同的索引集)在分配期间
我怎样才能让 Pandas 填写x['foo']
与另一个数据框中的另一列ignoring索引的对齐?
我能想到的最简单的方法pandas
忽略索引就是给它一些没有索引可以忽略的东西。从...开始
>>> x = pd.DataFrame({"foo": [10,20,30]},index=[1,2,0])
>>> y = pd.DataFrame({"bar": [33,11,22]},index=[0,1,2])
>>> x
foo
1 10
2 20
0 30
>>> y
bar
0 33
1 11
2 22
我们有通常的一致方法:
>>> x["foo"] = y["bar"].order(ascending=False)
>>> x
foo
1 11
2 22
0 33
或者是未对齐的,通过设置x["foo"]
到一个列表:
>>> x["foo"] = y["bar"].order(ascending=False).tolist()
>>> x
foo
1 33
2 22
0 11
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)