是否将ignore_index = True传递给pd.concat可以在我串联的数据帧中保留索引继承? [英] Will passing ignore_index=True to pd.concat preserve index succession within dataframes that I'm concatenating?

查看:30
本文介绍了是否将ignore_index = True传递给pd.concat可以在我串联的数据帧中保留索引继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据框:

df1 = 
    value
0     a
1     b
2     c

df2 =
    value
0     d
1     e

我需要通过索引将它们连接起来,但是我必须保留第一个数据帧的索引,并在第二个数据帧中继续它,如下所示:

I need to concatenate them across index, but I have to preserve the index of the first dataframe and continue it in the second dataframe, like this:

result =
    value
0     a
1     b
2     c
3     d
4     e

我的猜测是 pd.concat([df1,df2],ignore_index = True)会完成这项工作.但是,我担心对于大型数据帧,行的顺序可能会更改,而我最终会遇到这样的情况(前两行更改了索引):

My guess is that pd.concat([df1, df2], ignore_index=True) will do the job. However, I'm worried that for large dataframes the order of the rows may be changed and I'll end up with something like this (first two rows changed indices):

result =
    value
0     b
1     a
2     c
3     d
4     e

所以我的问题是,带有 ignore_index = True pd.concat 是否在要连接的数据帧中保存索引继承,或者索引分配中是否存在随机性?

So my question is, does the pd.concat with ignore_index=True save the index succession within dataframes that are being concatenated, or there is randomness in the index assignment?

推荐答案

以我的经验, pd.concat 可以在连接期间将DataFrames传递给行的顺序来对行进行连接.

In my experience, pd.concat concats the rows in the order the DataFrames are passed to it during concatenation.

如果您想安全起见,请指定 sort = False ,这还将避免对列进行排序:

If you want to be safe, specify sort=False which will also avoid sorting on columns:

pd.concat([df1, df2], axis=0, sort=False, ignore_index=True)

  value
0     a
1     b
2     c
3     d
4     e

这篇关于是否将ignore_index = True传递给pd.concat可以在我串联的数据帧中保留索引继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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