简体   繁体   English

为什么在 Pandas 中选择具有逻辑语句的行时,我必须指定 dataframe 两次?

[英]Why do I have to specify the dataframe twice while selecting rows with logical statements in Pandas?

I'm not sure what the reason might be for having to specify the name of the dataframe twice when selecting rows using conditional statements in Pandas.我不确定在使用 Pandas 中的条件语句选择行时必须两次指定 dataframe 的名称的原因是什么。 For example, if I have a dataframe df :例如,如果我有一个 dataframe df

name姓名 age年龄
Alice爱丽丝 31 31
Bob鲍勃 21 21

when I want to select rows with people over 30 I have to write over_thirty = df[df.age > 30] .当我想 select 行与 30 岁以上的人时,我必须写over_thirty = df[df.age > 30] Why not simply df['age' > 30]] ?为什么不简单地df['age' > 30]]

Use .query使用.query

over_thirty = df.query("age > 30")

so if you write df[age>3] it will give you output in true or false.所以如果你写 df[age>3] 它会给你 output 真或假。 I am sure which you not needed我确定你不需要哪个

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM