简体   繁体   English

根据来自另一个数据框的条件在一个数据框中找到行

[英]Locate rows in one dataframe based on criteria from another dataframe

I have created two Dataframes from a main dataframe, eg df1 and df2. 我已经从主数据帧(例如df1和df2)创建了两个数据帧。

Each dataframe has the same no of columns, but no of rows in df2 will be less than in df1. 每个数据框具有相同的列数,但df2中的行数少于df1中的行数。 The dataframe will have columns waferlot,x,y,w. 数据框将具有一列waferlot,x,y,w。

How can search for waferlot,x,y,w from df2 in df1. 如何在df1中从df2搜索晶圆,x,y,w。

While there can be multiple ways to achieve this, one of which has been mentioned in the comments, but I usually do it using isin : 尽管有多种方法可以实现此目的,但注释中已经提到了其中一种,但是我通常使用isin来实现

Given two dataframes: 给定两个数据框:

import pandas as pd

df1 = pd.DataFrame()
df1['C1'] = ['a', 'b', 'c', 'd', 'e', 'f']
df1['C2'] = ['b', 'c', 'x', 'w', 'h', 'j']
df2 = pd.DataFrame()
df2 ['C1'] = ['x', 'a', 'c', 'f']
df2 ['C2'] = ['w', 'h', 'd', 'j']

The dataframes look like: 数据帧如下所示:

In [144]: df1
Out[144]:
  C1 C2
0  a  b
1  b  c
2  c  x
3  d  w
4  e  h
5  f  j

In [145]: df2
Out[145]:
  C1 C2
0  x  w
1  a  h
2  c  d
3  f  j

Now, I can use isin to search for whatever I want across all columns of the dataframe. 现在,我可以使用isin在数据框的所有列中搜索所需的内容。

x = (df1[df1.C1.isin(df2.C1) & df1.C2.isin(df2.C2)])

Output: 输出:

  C1 C2
5  f  j

If you want to search on one column only then, you can remove one of the conditions before or after the & : 如果仅要搜索一列,则可以删除&之前或之后的条件之一:

x = (df1[df1.C1.isin(df2.C1)])

Output: 输出:

  C1 C2
0  a  b
2  c  x
5  f  j

暂无
暂无

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

相关问题 根据另一个数据帧的条件选择行 - Selecting rows based on criteria from another dataframe 基于两个标准将值从一个数据帧映射到另一个数据帧 - Map Values from one dataframe to another based two criteria Pandas DataFrames:如何根据另一个数据帧列中的值使用现有数据帧中的索引值定位行? - Pandas DataFrames: How to locate rows using index values in existing dataframe based on values from another dataframe column? 根据另一个DataFrame中的行选择一个DataFrame中的行 - Select rows in one DataFrame based on rows in another 根据另一个数据框中的值从DataFrame中选择行,并根据第二个DataFrame使用值更新其中一个列 - Select rows from a DataFrame based on a values in another dataframe and updating one of the column with values according to the second DataFrame 从基于另一个数据帧的一个数据框中进行选择 - choose from one dataframe based on another dataframe 根据来自另一个 DataFrame 的标准过滤 Pandas 中的 DataFrame - Filtering DataFrame in pandas based on criteria from another DataFrame 根据来自另一个数据框的 2 个条件创建新的数据框列 - Create new dataframe column based on 2 criteria from another dataframe 一个 dataframe 中的值基于另一个行中的条件 - Looing values in one dataframe based on conditions from rows in another 熊猫:根据时间条件将行从一个数据框映射到另一个数据框 - Pandas: Map rows from one dataframe to another based on a time condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM