简体   繁体   English

如何根据特定行的列值查询 pandas 数据帧

[英]How to query on a pandas data frame based on column values of specific row

I have this dataframe:我有这个 dataframe:

在此处输入图像描述

and I would like to select all rows which have the same values for 'x1' and 'x2' as the first row.我想 select 所有与第一行具有相同“x1”和“x2”值的行。

I know that I can query the data frame after I get the values of x1 and x2 for this code.我知道在获得此代码的 x1 和 x2 的值后,我可以查询数据框。 However, I want more general way if I had more features than x1 and x2, say I would like to do the same on 100 feature.但是,如果我有比 x1 和 x2 更多的功能,我想要更通用的方法,比如我想在 100 个功能上做同样的事情。 is there a way that I can take the similar values?有没有办法可以采用类似的值?

I also thought about group by, then choose the group which contains this row, but I think ofc there will be other better ways我也考虑过group by,然后选择包含这一行的组,但我认为ofc会有其他更好的方法

Try:尝试:

cols = ['x1','x2']
df[df[cols].eq(df.iloc[0][cols]).all(1)]

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

相关问题 根据最终 excel 文件中特定列中的空/空白值删除一行 - Pandas 数据框 - Drop a row based on empty/blanks values in specific column in final excel file - Pandas Data frame 使用python更改特定pandas数据框列中的行值 - Change row values in specific pandas data frame column with python 如何在特定的熊猫数据框列中查找值,然后将该行中的其他值存储在单独的变量中 - How to look for a value in a specific pandas data frame column and then store the other values from that row in separate variables 如何基于Pandas数据框中的top-K值识别行和列 - How to identify row and column based on top-K values in Pandas data frame 如何替换熊猫数据框中单个行和特定列的值 - how to replace values of an individual row and specific columns in pandas data frame 如何使用 pandas 根据前一行值填充数据框? - How to populate data frame based on previous row values with pandas? 如何在特定索引处重复熊猫数据框列中的值? - How to repeat values in pandas data frame column at specific indices? 如何使用 pandas 根据列的值范围分隔数据框? - How to separate a data frame based on a column's range of values with pandas? 如何根据其他行值添加 pandas 数据框列 - How to add pandas data frame column based on other rows values Pandas:基于列值聚合数据框 - Pandas: Aggregate data frame based on column values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM