简体   繁体   English

使用dataframes列以相同顺序从另一个框架中选择行

[英]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 . 考虑两个数据帧AB 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 . 但这似乎不是解决方案-结果中的列idA的值顺序不同。

You could use pd.merge 您可以使用pd.merge

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 select 行来自 pandas 数据帧,基于另一列 % 出现百分比 - select rows from pandas data frame based on another column % percentage of occurrence 根据相同的标题前缀从多个 DataFrame 中选择列 - Select column from multiple DataFrames based on same header prefix 根据另一个范围从熊猫数据框中选择行 - Select rows from pandas data frame based on range from another 从另一个(相同的行数)数据框中按列分组 - Groupby a column from another (same # of rows) dataframe 来自 Pandas DataFrame 的 Select 行与另一个 DataFrame 中的列值完全相同 - Select rows from a Pandas DataFrame with exactly the same column values in another DataFrame 将一个数据框中的一列添加到另一个具有不相等行的列 - Adding a column from one data frame to another with unequal rows 如何有条件地根据同一数据帧另一列中的值对Pandas数据帧中的行进行计数? - How to count rows in a data frame in Pandas conditionally against values in another column of the same data frame? Pandas 数据帧 - 从另一个 dataframe 的字符串列中的一个数据帧中搜索 integer - Pandas Dataframes - Search an integer from one data frame in a string column in another dataframe 用另一个数据框 pandas 重命名每 2 列 - Rename every 2 column with same from another data frame pandas 从数据框中选择行,其中两个元素在另一个数据框中的一行中匹配 - Select rows from a data frame where two elements match on a single row from another data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM