[英]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.