[英]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
. 考虑两个数据帧
A
和B
Both contain a column id
with identifier values: 两者都包含带有标识符值的列
id
:
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
. 我需要一个版本数据帧的
B
仅含有包含来自标识符的行A[id]
在完全相同的顺序,如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
. 但这似乎不是解决方案-结果中的列
id
与A
的值顺序不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.