简体   繁体   English

Pandas:返回数据框,其中一列的值大于另一列

[英]Pandas: return dataframe where one column's values are greater than another

Apologies if this is a duplicate but I can't seem to find a working example in the pandas docs, SO or google. 如果这是重复的道歉,但我似乎无法在pandas docs,SO或google中找到一个有效的例子。

How do you return a dataframe where the values of one column are greater than the values of another? 如何返回一列的值大于另一列的值的数据帧?

Should be something like this: df['A'].where(df['A']>df['B']) 应该是这样的: df['A'].where(df['A']>df['B'])

But this returns only a vector. 但这只返回一个向量。 I need the full filtered dataframe. 我需要完整的过滤数据帧。

Try using query 尝试使用query

df.query('A > B')

consider df 考虑df

np.random.seed([3,1415])
df = pd.DataFrame(np.random.rand(10, 2), columns=list('AB'))
df

在此输入图像描述

option 1 选项1

df.query('A > B')

option 2 选项2

df[df.A.gt(df.B)]

在此输入图像描述

To do df['A'].where(df['A']>df['B']) in pandas syntax is essentially a mask. 要做df['A'].where(df['A']>df['B']) pandas语法本质上是一个掩码。 Instead of where you are taking a subset of the dataframe: 而不是你在where采取数据帧的子集:

df[df['A'] > df['B']]

例

暂无
暂无

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

相关问题 过滤熊猫数据框中的行,其中列中的值大于 x 或 NaN - Filter for rows in pandas dataframe where values in a column are greater than x or NaN Pandas dataframe - 选择一列的值包含字符串,另一列的值以特定字符串开头的行 - Pandas dataframe - Select rows where one column's values contains a string and another column's values starts with specific strings Pandas dataframe,最大值,其中列值大于变量 - Pandas dataframe, max value where column value is greater than variable Pandas-将列除以另一列,条件是值是否大于0? - Pandas- Dividing a column by another column conditional on if values are greater than 0? 如何将一列除以另一列,其中一个数据帧的列值对应于 Python Pandas 中另一个数据帧的列值? - How to divide one column by another where one dataframe's column value corresponds to another dataframe's column's value in Python Pandas? 熊猫从第二个数据框中选择的列,其中另一个列的值存在于主数据框中 - pandas selected columns from second dataframe where another column's values exist in a primary dataframe 替换熊猫数据框中大于数字的值 - Replacing values greater than a number in pandas dataframe 熊猫数据框合并其中 1 列匹配,但另一列的值不存在 - Pandas Dataframe Merge Where 1 Column Matches, but Another Column's Values are not Present 检查特定列是否大于另一列并根据 pandas dataframe 中的条件创建新列 - Check if specific column is greater than another column and create a new column based on condition in pandas dataframe pandas数据框根据另一数据框中的值将值追加到一列 - pandas dataframe append values to one column based on the values in another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM