Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 熊猫中的布尔子集_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 熊猫中的布尔子集

Python 3.x 熊猫中的布尔子集,python-3.x,pandas,Python 3.x,Pandas,在学习熊猫跟随“熊猫为大家”之后,遇到这样一个例子 #+BEGIN_SRC python :results output :session print(scientists) #+END_SRC #+RESULTS: Name Born Died Age Occupation 0 Rosaline Franklin 1920-07-

在学习熊猫跟随“熊猫为大家”之后,遇到这样一个例子

#+BEGIN_SRC  python :results output  :session
print(scientists)
#+END_SRC

#+RESULTS:
                   Name                               Born        Died        Age          Occupation
0     Rosaline Franklin     1920-07-25  1958-04-16   37             Chemist
1        William Gosset        1876-06-13  1937-10-16   61        Statistician
2  Florence Nightingale  1820-05-12  1910-08-13   90               Nurse
3           Marie Curie            1867-11-07  1934-07-04   66             Chemist
4         Rachel Carson        1907-05-27  1964-04-14   56           Biologist
5             John Snow           1813-03-15  1858-06-16   45           Physician
6           Alan Turing            1912-06-23  1954-06-07   41  Computer Scientist
7          Johann Gauss         1777-04-30  1855-02-23   77       Mathematician
一种布尔运算

#+BEGIN_SRC  python :results output  :session
# boolean vectors will subset rows
print(scientists[scientists['Age'] > scientists['Age'].mean()])
#+END_SRC

#+RESULTS:
:                    Name        Born        Died  Age     Occupation
: 1        William Gosset        1876-06-13  1937-10-16   61   Statistician
: 2  Florence Nightingale  1820-05-12  1910-08-13   90          Nurse
: 3           Marie Curie            1867-11-07  1934-07-04   66        Chemist
: 7          Johann Gauss       1777-04-30  1855-02-23   77  Mathematician
然后是一个混乱的操作,它指出:

因为广播是如何工作的,如果我们提供一个布尔向量 与数据帧中的行数相同,为最大行数 返回的将是布尔向量的长度


结果把我弄糊涂了,
[[True,True,False,True]]
映射到什么?

这意味着您通过布尔掩码-行按布尔序列、列表或数组过滤-仅返回带
True
的行-因此在索引为
0,1,3
的数据中

在pandas 0.24+中进行测试后,如果行数高于布尔掩码中的值数,则其工作无误:

df1 = pd.DataFrame({'a': range(6)}) 
print (df1)
   a
0  0
1  1
2  2
3  3
4  4
5  5

print(df1.loc[[True, True, False, True]])
   a
0  0
1  1
3  3

你的数据格式混乱了,你能重新格式化它吗?这样我们才能真正理解我们在看什么?很明显,
[True,True,False,True]
被映射到数据帧的前4行,然后它只是普通的布尔索引。
df1 = pd.DataFrame({'a': range(6)}) 
print (df1)
   a
0  0
1  1
2  2
3  3
4  4
5  5

print(df1.loc[[True, True, False, True]])
   a
0  0
1  1
3  3