Pandas dataframe.loc:“与行轴长度相同的布尔列表”是什么意思?

2024-05-16 23:17:59 发布

您现在位置:Python中文网/ 问答频道 /正文

熊猫文档有这样一个Boolean list with the same length as the row axis示例,如下所示:

>>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
...      index=['cobra', 'viper', 'sidewinder'],
...      columns=['max_speed', 'shield'])
>>> df
            max_speed  shield
cobra               1       2
viper               4       5
sidewinder          7       8


>>> df.loc[[False, False, True]]
            max_speed  shield
sidewinder          7       8

有人能解释一下df.loc[[False, False, True]]是如何得到显示的结果的吗

谢谢


Tags: the文档falsetruedfwithlocmax
1条回答
网友
1楼 · 发布于 2024-05-16 23:17:59

df[[False,False,True]]返回与df.loc[[False, False, True]]相同的值

df[[False,False,True]]更直观&;可以解释为:不要先返回&;第二行(即第一行和第二行False),返回第三行(即第三行是True

In the documentiation,请参见要点:

  • A boolean array of the same length as the axis being sliced, e.g. [True, False, True].

相关问题 更多 >