问题:如何合并两个数据帧?
我正在使用Pandas数据框。我说有一个初始数据框D
。我从中提取两个数据帧,如下所示:
A = D[D.label == k]
B = D[D.label != k]
然后我更改标签中A
和B
A.label = 1
B.label = -1
我想将A和B结合起来,这样我就可以将它们作为一个数据帧使用,类似于联合操作。数据的顺序并不重要。但是,当我们从D采样A和B时,它们保留了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]
I want to combine A
and B
so I can have them as one DataFrame, 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
.
回答 0
我相信你可以使用该append
方法
bigdata = data1.append(data2, ignore_index=True)
保持索引只是不使用ignore_index
关键字…
I believe you can use the append
method
bigdata = data1.append(data2, ignore_index=True)
to keep their indexes just dont use the ignore_index
keyword …
回答 1
您还可以使用pd.concat
,当您连接两个以上数据框时,这特别有用:
bigdata = pd.concat([data1, data2], ignore_index=True, sort=False)
You can also use pd.concat
, which is particularly helpful when you are joining more than two dataframes:
bigdata = pd.concat([data1, data2], ignore_index=True, sort=False)
回答 2
如果有人发现它有用,可以考虑在此处添加它。@ostrokach已经提到了如何合并跨行的数据框,即
df_row_merged = pd.concat([df_a, df_b], ignore_index=True)
要跨列合并,可以使用以下语法:
df_col_merged = pd.concat([df_a, df_b], axis=1)
Thought to add this here in case someone finds it useful. @ostrokach already mentioned how you can merge the data frames across rows which is
df_row_merged = pd.concat([df_a, df_b], ignore_index=True)
To merge across columns, you can use the following syntax:
df_col_merged = pd.concat([df_a, df_b], axis=1)
回答 3
对于正在处理大数据并需要连接多个数据集的情况,还有另一种解决方案。concat
可能会提高性能,因此,如果您不想每次都创建新的df,则可以使用列表推导:
frames = [ process_file(f) for f in dataset_files ]
result = pd.append(frames)
(如本节底部文档中的此处所指出):
注意:但是,值得注意的是,concat
(并因此append
)制作了数据的完整副本,并且不断地重用此功能可能会严重影响性能。如果需要对多个数据集使用该操作,请使用列表推导。
There’s another solution for the case that you are working with big data and need to concatenate multiple datasets. concat
can get performance-intensive, so if you don’t want to create a new df each time, you can instead use a list comprehension:
frames = [ process_file(f) for f in dataset_files ]
result = pd.append(frames)
(as pointed out here in the docs at the bottom of the section):
Note: It is worth noting however, that concat
(and therefore append
)
makes a full copy of the data, and that constantly reusing this
function can create a significant performance hit. If you need to use
the operation over several datasets, use a list comprehension.
回答 4
如果要用df1
第二个数据帧的值更新/替换第一个数据帧的值df2
。您可以按照以下步骤进行操作-
步骤1:设置第一个数据帧(df1)的索引
df1.set_index('id')
步骤2:设置第二个数据帧(df2)的索引
df2.set_index('id')
最后使用以下代码段更新数据框-
df1.update(df2)
If you want to update/replace the values of first dataframe df1
with the values of second dataframe df2
. you can do it by following steps —
Step 1: Set index of the first dataframe (df1)
df1.set_index('id')
Step 2: Set index of the second dataframe (df2)
df2.set_index('id')
and finally update the dataframe using the following snippet —
df1.update(df2)
回答 5
第一个数据帧
train.shape
结果:-
(31962, 3)
第二个数据帧
test.shape
结果:-
(17197, 2)
结合
new_data=train.append(test,ignore_index=True)
检查一下
new_data.shape
结果:-
(49159, 3)
1st dataFrame
train.shape
result:-
(31962, 3)
2nd dataFrame
test.shape
result:-
(17197, 2)
Combine
new_data=train.append(test,ignore_index=True)
Check
new_data.shape
result:-
(49159, 3)