Python pandas concat 连接时指定索引顺序

Python pandas concat 连接时指定索引顺序

一些旧的教材上,在使用concat连接时,使用join_axes参数指定顺序,但这已经过时了,因为报错。

>>> import pandas as pd
>>> 
>>> one = pd.DataFrame([[0, 1], [2, 3]], columns=list('ab'))
>>> two = pd.DataFrame([[10, 11], [12, 13]], index=[1, 2], columns=list('bc'))
>>> pd.concat([one, two], join='outer', axis=1, join_axes=two.index)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: concat() got an unexpected keyword argument 'join_axes'

原因是在pandas 0.25后就丢弃了这个参数

具体在0.25版本说明:https://pandas.pydata.org/docs/whatsnew/v0.25.0.html#other-deprecations

在这里插入图片描述

这样的话,可以使用reindex来指定索引顺序

>>> pd.concat([one, two], axis=1).reindex(two.index)
     a    b     b     c
1  2.0  3.0  10.0  11.0
2  NaN  NaN  12.0  13.0
posted @ 2022-04-07 11:22  王舰  阅读(700)  评论(0)    收藏  举报