Pandas 属性的行为与预期的不一样

Pandas 属性的行为与预期的不一样,pandas,dataframe,loc,Pandas,Dataframe,Loc,请有人解释一下为什么loc的行为与我预期的不同。代码是 educated_less = df.loc[ ~df['education'].isin(['Masters', 'Bachelors', 'Doctorate'])] 然而,似乎loc应该在isin条件之后只返回一列“教育” 它返回包含所有列的整个df数据帧,并应用isin条件 Thnx 因为索引有两部分: df.loc[left_part, right_part] left_part <- where you define

请有人解释一下为什么loc的行为与我预期的不同。代码是

educated_less = df.loc[ ~df['education'].isin(['Masters', 'Bachelors', 'Doctorate'])]
然而,似乎loc应该在isin条件之后只返回一列“教育” 它返回包含所有列的整个df数据帧,并应用isin条件

Thnx

因为索引有两部分:

df.loc[left_part, right_part]

left_part <- where you define by which index to filter
right_part <- where you define which columns you want to keep

欢迎Nikita,我们鼓励您在发布答案之前研究您的问题。请阅读上的pandas用户指南部分,注意
.loc
如何选择行和列索引,如
.loc[rows,columns]
中所示,您正在使用
.loc[rows]
跳过列参数,那么默认情况下将返回所有列
educated_less = df.loc[ ~df['education'].isin(['Masters', 'Bachelors', 'Doctorate']), ['education']]