假设我有两个数据帧x并且y在Pandas中,我想填写一个列,x其中包含对列进行排序的结果y.我试过这个:
x['foo'] = y['bar'].order(ascending=False)
Run Code Online (Sandbox Code Playgroud)
但它没有工作,我怀疑是因为熊猫对齐指数之间x以及y在分配期间(其具有相同的索引集)
我怎样才能让Pandas x['foo'] 用另一个数据框中的另一列填充而忽略索引的对齐?
我能想到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
Run Code Online (Sandbox Code Playgroud)
我们采用通常的一致方法:
>>> x["foo"] = y["bar"].order(ascending=False)
>>> x
foo
1 11
2 22
0 33
Run Code Online (Sandbox Code Playgroud)
或者是未对齐的,通过设置x["foo"]列表:
>>> x["foo"] = y["bar"].order(ascending=False).tolist()
>>> x
foo
1 33
2 22
0 11
Run Code Online (Sandbox Code Playgroud)