![](/img/trans.png)
[英]How can I make a merge of two data frames taking The first row that match or the next one
[英]How can I merge data frames by using the indexing of one of them?
我有两个数据帧(A和B),如下所示:
*类型为:
<class 'pandas.core.frame.DataFrame'>
---> A
<class 'pandas.core.frame.DataFrame'>
---> B
A:
target
145 1
557 1
240 1
893 1
1518 0
1145 0
B:
RF LR NB DT SVM Knn SUM
0 1 0 0 1 1 1 4
1 1 1 1 1 0 1 5
2 1 1 1 1 1 1 6
3 1 1 1 1 1 1 6
4 1 0 0 1 0 0 2
5 1 1 1 0 1 1 5
我需要一个包含两个数据框。
如何使用A的索引将它们合并在一起(按列)(而忽略B的索引)?
IIUC,你可以删除索引A
, join
或concat
两个框架,并重置指数是A
的指标:
A.reset_index(drop=True).join(B).set_index(A.index)
# or
pd.concat((A.reset_index(drop=True), B),axis=1).set_index(A.index)
target RF LR NB DT SVM Knn SUM
145 1 1 0 0 1 1 1 4
557 1 1 1 1 1 0 1 5
240 1 1 1 1 1 1 1 6
893 1 1 1 1 1 1 1 6
1518 0 1 0 0 1 0 0 2
1145 0 1 1 1 0 1 1 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.