Pandas以特定订单FAST检索数据

问题描述 投票:1回答:1

我希望以ID列值的特定顺序一次获得多个条目。为了使事情变得更复杂,作为输入我有ID1和ID2的行,并且对于每一行,ID1或ID2在表中但不是两者。

ID都是唯一的。

import pandas as pd
import numpy as np

print('Generating table and matchTable...')

N = 10000
# General unique IDs list to draw from
ids = np.random.choice(a=list(range(N*100)), replace=False, size=N*10)

# First N ids go into MAIN_IDS
mainIDs = ids[:N]
data = np.random.randint(low=0, high=25, size=N)

table = pd.DataFrame({'MAIN_IDS': mainIDs, 'DATA':data})

# These ids exist in the table as MAIN_IDS
tableIdsList = np.random.choice(mainIDs, replace=False, size=int(N/10))
notInTableIdsList = ids[N:N+int(N/10)]

idsA = np.zeros(shape=(int(N/10)), dtype=np.int)
idsB = np.zeros(shape=(int(N/10)), dtype=np.int)
for i in range(len(idsA)):
    if np.random.random()>0.4:
        idsA[i] = tableIdsList[i]
        idsB[i] = notInTableIdsList[i]
    else:
        idsA[i] = notInTableIdsList[i]
        idsB[i] = tableIdsList[i]

matchTable = pd.DataFrame({'ID1': idsA, 'ID2':idsB})
print('   Done!')

print('Generating the correct result...')
correctResult = []
for i in range(len(tableIdsList)):
    correctResult.append(data[np.where(mainIDs==tableIdsList[i])[0][0]])
correctResult = np.array(correctResult)
print('   Done!')

我想得到DATA,MAIN_ID == ID1或ID2,但是匹配matchTable。

python pandas
1个回答
2
投票

首先按表格中的Id过滤您的匹配表,然后我们使用reindex

idx=matchTable.where(matchTable.isin(table.MAIN_IDS.tolist())).stack()

table=table.set_index('MAIN_IDS').reindex(idx).reset_index()
© www.soinside.com 2019 - 2024. All rights reserved.