python - 布尔索引的差异取决于索引符号
问题描述
我在熊猫中遇到了一些奇怪的事情,并试图找出原因。我基于应用于年龄列的简单 if/else 函数创建了一个新列:
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/04_Apply/Students_Alcohol_Consumption/student-mat.csv')
def majority(x):
if x >= 18:
return True
else:
return False
df['legal_drinker'] = df.age.apply(majority)
但是现在,根据我的索引方式,我得到了两个不同的答案:
df[df['legal_drinker'] == True].shape
>>(111, 13)
df[df.legal_drinker == True].shape`
>>(1, 13)
有人知道为什么会这样吗?这完全让我困惑。
解决方案
推荐阅读
- python - How to get value from the output of a weighing scale
- ruby - What does ‘execute_script(script, *args)' mean in ruby Selenium web driver
- swift - How to create the operator "|>" to sort the collection [2,5,3,4] from least to greatest on swift
- elasticsearch - Elasticsearch:更喜欢前缀匹配而不是术语匹配
- matplotlib - Create a LinearSegmentedColormap with values within range
- arrays - 如何编写 N1QL 查询以获取所有 ordrqty 等于 102 的 SaleDt1
- node.js - 从 firebase 实时数据库中删除
- javascript - 尝试构建 React-Admin,不断收到“编辑 src/App.js 并保存以重新加载”
- asp.net-web-api - 我尝试使用 IApplicationBuilder 制作 Asp.net Api,而不是 asp.net 核心
- laravel - 重定向 301 不起作用 htaccess laravel