Christian Staudt Christian Staudt - 11 days ago 6
Python Question

Use a dataframes column to select rows from another frame in the same order

Need some pandas jump start here:

Consider two data frames

A
and
B
. Both contain a column
id
with identifier values:

A: id valA
8 ?
2 ?
4 ?

B: id valB valC
1 ? ?
4 ? ?
3 ? ?
8 ? ?
2 ? ?


I need a version of dataframe
B
which contains only the rows that contain the identifiers from
A[id]
, in exactly the same order as in
A
. That is:

B': id valB valC
8 ? ?
2 ? ?
4 ? ?


I assume this is a basic operation in pandas, but it seems I am missing the vocabulary to google it right now.

I've tried

B.loc[B['id'].isin(A['id'])]


but that doesn't seem to be the solution - the column
id
in the result doesn't have the same order of values as in
A
.

Answer

You could use pd.merge

In [92]: A.merge(B)
Out[92]:
   id valA valB valC
0   8    ?    ?    ?
1   2    ?    ?    ?
2   4    ?    ?    ?