pandas concat ignore_index不起作用 [英] pandas concat ignore_index doesn't work

查看:208
本文介绍了 pandas concat ignore_index不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对数据框进行列绑定,并遇到熊猫concat的问题,因为ignore_index=True似乎不起作用:

I am trying to column-bind dataframes and having issue with pandas concat, as ignore_index=True doesn't seem to work:

df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3'],
                    'D': ['D0', 'D1', 'D2', 'D3']},
                    index=[0, 2, 3,4])

df2 = pd.DataFrame({'A1': ['A4', 'A5', 'A6', 'A7'],
                    'C': ['C4', 'C5', 'C6', 'C7'],
                    'D2': ['D4', 'D5', 'D6', 'D7']},
                    index=[ 5, 6, 7,3])
df1
#     A   B   D
# 0  A0  B0  D0
# 2  A1  B1  D1
# 3  A2  B2  D2
# 4  A3  B3  D3

df2
#    A1   C  D2
# 5  A4  C4  D4
# 6  A5  C5  D5
# 7  A6  C6  D6
# 3  A7  C7  D7

dfs = [df1,df2]
df = pd.concat( dfs,axis=1,ignore_index=True)     
print df   

结果是

     0    1    2    3    4    5    
0   A0   B0   D0  NaN  NaN  NaN  
2   A1   B1   D1  NaN  NaN  NaN    
3   A2   B2   D2   A7   C7   D7   
4   A3   B3   D3  NaN  NaN  NaN  
5  NaN  NaN  NaN   A4   C4   D4  
6  NaN  NaN  NaN   A5   C5   D5  
7  NaN  NaN  NaN   A6   C6   D6           

即使我使用重置索引

 df1.reset_index()    
 df2.reset_index() 

然后尝试

pd.concat([df1,df2],axis=1) 

它仍然产生相同的结果!

it still produces the same result!

推荐答案

如果我正确理解了您的意思,这就是您想要做的.

If I understood you correctly, this is what you would like to do.

import pandas as pd

df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3'],
                    'D': ['D0', 'D1', 'D2', 'D3']},
                    index=[0, 2, 3,4])

df2 = pd.DataFrame({'A1': ['A4', 'A5', 'A6', 'A7'],
                    'C': ['C4', 'C5', 'C6', 'C7'],
                    'D2': ['D4', 'D5', 'D6', 'D7']},
                    index=[ 4, 5, 6 ,7])


df1.reset_index(drop=True, inplace=True)
df2.reset_index(drop=True, inplace=True)

df = pd.concat( [df1, df2], axis=1) 

哪个给:

    A   B   D   A1  C   D2
0   A0  B0  D0  A4  C4  D4
1   A1  B1  D1  A5  C5  D5
2   A2  B2  D2  A6  C6  D6
3   A3  B3  D3  A7  C7  D7

实际上,我希望df = pd.concat(dfs,axis=1,ignore_index=True)会得到相同的结果.

Actually, I would have expected that df = pd.concat(dfs,axis=1,ignore_index=True) gives the same result.

这是 jreback 的出色解释:

ignore_index=True忽略",表示未在连接轴上对齐.只需按照传递顺序将它们粘贴在一起,然后为实际索引重新分配一个范围(例如range(len(index))) 因此,联接非重叠索引(在示例中假设为axis=1)之间的区别在于,使用ignore_index=False(默认值),您可以得到索引的连接,而使用ignore_index=True,您可以得到范围. /p>

ignore_index=True ‘ignores’, meaning doesn’t align on the joining axis. it simply pastes them together in the order that they are passed, then reassigns a range for the actual index (e.g. range(len(index))) so the difference between joining on non-overlapping indexes (assume axis=1 in the example), is that with ignore_index=False (the default), you get the concat of the indexes, and with ignore_index=True you get a range.

这篇关于 pandas concat ignore_index不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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