gpt4 book ai didi

python - 根据另一个数据帧的索引保留一个数据帧的索引

转载 作者:行者123 更新时间:2023-12-01 00:33:46 25 4
gpt4 key购买 nike

假设我有两个数据帧 AB,索引从 010。我从 A 中删除了几行重复​​的行,以便删除索引 79 。所以现在 A.index 将是 [0,1,2,3,4,5,6,8,10]

现在我想准确保留 B 中具有相同索引的行。截至目前,其索引集从 010。换句话说,给定最初完全相同的索引,并且从 A 中删除了一些索引,如何保留与保留的行完全对应的 B 行的子集A 就其索引而言?

最佳答案

我相信您可以通过loc进行选择:

A = pd.DataFrame({'col':[5,8,4,0,6,2,1,8,3,4,9]})
B = pd.DataFrame({'col':np.arange(10, 21)})
#print (A)
#print (B)

A1 = A.drop_duplicates('col')
print (A1)
col
0 5
1 8
2 4
3 0
4 6
5 2
6 1
8 3
10 9

B1 = B.loc[A1.index]
print (B1)
col
0 10
1 11
2 12
3 13
4 14
5 15
6 16
8 18
10 20

关于python - 根据另一个数据帧的索引保留一个数据帧的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57969720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com