如何合并两个数据帧? [英] How do I combine two data frames?

查看:79
本文介绍了如何合并两个数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Pandas数据框.我有一个初始数据帧,例如D.我像这样从中提取两个数据帧:

I'm using Pandas data frames. I have a initial data frame, say D. I extract two data frames from it like this:

A = D[D.label == k]
B = D[D.label != k]

然后我更改AB

A.label = 1
B.label = -1

我想将A和B组合在一起,这样我就可以将它们作为一个数据帧,类似于联合操作.数据的顺序并不重要.但是,当我们从D采样A和B时,它们保留了D的索引.

I want to combine A and B so I can have them as one data frame, something like a union operation. The order of the data is not important. However, when we sample A and B from D, they retain their indexes from D.

推荐答案

我相信您可以使用append方法

bigdata = data1.append(data2, ignore_index=True)

要保留其索引,只是不要使用ignore_index关键字...

to keep their indexes just dont use the ignore_index keyword ...

这篇关于如何合并两个数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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