简体   繁体   English

如何使用其中之一的索引合并数据帧?

[英]How can I merge data frames by using the indexing of one of them?

I have two data frames (A and B) as following: 我有两个数据帧(A和B),如下所示:

*The types are: *类型为:

<class 'pandas.core.frame.DataFrame'> ---> A <class 'pandas.core.frame.DataFrame'> ---> A

<class 'pandas.core.frame.DataFrame'> ---> B <class 'pandas.core.frame.DataFrame'> ---> B

A: A:

         target
145        1
557        1
240        1
893        1
1518       0
1145       0

B: 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

I need one data frame that will include both. 我需要一个包含两个数据框。

How can I merge them together (by columns) by using the indexing of A (and ignore the indexing of B) ? 如何使用A的索引将它们合并在一起(按列)(而忽略B的索引)?

IIUC, you can drop the index from A , join or concat the two frames, and reset the index to be A 's index: IIUC,你可以删除索引Ajoinconcat两个框架,并重置指数是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM