将 ignore_index=True 传递给 pd.concat 会保留我正在连接的数据帧中的索引继承吗?
Will passing ignore_index=True to pd.concat preserve index succession within dataframes that I'm concatenating?
我有两个数据框:
df1 =
value
0 a
1 b
2 c
df2 =
value
0 d
1 e
我需要跨索引连接它们,但我必须保留第一个数据帧的索引并在第二个数据帧中继续它,如下所示:
result =
value
0 a
1 b
2 c
3 d
4 e
我的猜测是 pd.concat([df1, df2], ignore_index=True)
可以完成这项工作。但是,我担心对于大型数据帧,行的顺序可能会改变,我最终会得到这样的结果(前两行改变了索引):
result =
value
0 b
1 a
2 c
3 d
4 e
所以我的问题是,pd.concat
和 ignore_index=True
是在连接的数据帧中保存索引序列,还是在索引分配中存在随机性?
根据我的经验,pd.concat
按照连接期间 DataFrame 传递给它的顺序连接行。
如果您想安全起见,请指定 sort=False
这也将避免按列排序:
pd.concat([df1, df2], axis=0, sort=False, ignore_index=True)
value
0 a
1 b
2 c
3 d
4 e
我有两个数据框:
df1 =
value
0 a
1 b
2 c
df2 =
value
0 d
1 e
我需要跨索引连接它们,但我必须保留第一个数据帧的索引并在第二个数据帧中继续它,如下所示:
result =
value
0 a
1 b
2 c
3 d
4 e
我的猜测是 pd.concat([df1, df2], ignore_index=True)
可以完成这项工作。但是,我担心对于大型数据帧,行的顺序可能会改变,我最终会得到这样的结果(前两行改变了索引):
result =
value
0 b
1 a
2 c
3 d
4 e
所以我的问题是,pd.concat
和 ignore_index=True
是在连接的数据帧中保存索引序列,还是在索引分配中存在随机性?
根据我的经验,pd.concat
按照连接期间 DataFrame 传递给它的顺序连接行。
如果您想安全起见,请指定 sort=False
这也将避免按列排序:
pd.concat([df1, df2], axis=0, sort=False, ignore_index=True)
value
0 a
1 b
2 c
3 d
4 e