Python教程

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

本文主要是介绍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
这篇关于Python pandas concat 连接时指定索引顺序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!