给定一个列为"BoolCol"的数据帧,我们希望找到数据帧的索引,其中"BoolCol"的值=True

我目前有一种迭代的方法来做这件事,它非常有效:

for i in range(100,3000):
    if df.iloc[i]['BoolCol']== True:
         print i,df.iloc[i]['BoolCol']

但这不是Pandas 的正确做法. 经过一些研究,我目前正在使用这个代码:

df[df['BoolCol'] == True].index.tolist()

这一个给了我一个索引列表,但当我通过以下方式判断它们时,它们不匹配:

df.iloc[i]['BoolCol']

结果其实是假的!!

哪种方法是正确的?

推荐答案

df.iloc[i]返回df的第ith行.i不是指索引标签,i是从0开始的索引.

相比之下,the attribute 100 returns actual index labels,而不是数字行索引:

df.index[df['BoolCol'] == True].tolist()

或者相当于,

df.index[df['BoolCol']].tolist()

通过使用DataFrame,您可以非常清楚地看到其中的不同之处 与行的数字位置不相等的非默认索引:

df = pd.DataFrame({'BoolCol': [True, False, False, True, True]},
       index=[10,20,30,40,50])

In [53]: df
Out[53]: 
   BoolCol
10    True
20   False
30   False
40    True
50    True

[5 rows x 1 columns]

In [54]: df.index[df['BoolCol']].tolist()
Out[54]: [10, 40, 50]

If you want to use the index

In [56]: idx = df.index[df['BoolCol']]

In [57]: idx
Out[57]: Int64Index([10, 40, 50], dtype='int64')

then you can select the rows using 100 instead of 101:

In [58]: df.loc[idx]
Out[58]: 
   BoolCol
10    True
40    True
50    True

[3 rows x 1 columns]

注意100 can also accept boolean arrays:

In [55]: df.loc[df['BoolCol']]
Out[55]: 
   BoolCol
10    True
40    True
50    True

[3 rows x 1 columns]

If you have a boolean array, 100, and need ordinal index values, you can compute them using 101:

In [110]: np.flatnonzero(df['BoolCol'])
Out[112]: array([0, 3, 4])

使用df.iloc按顺序索引 Select 行:

In [113]: df.iloc[np.flatnonzero(df['BoolCol'])]
Out[113]: 
   BoolCol
10    True
40    True
50    True

Python相关问答推荐

使用imap-tools时错误,其邮箱地址包含域名中的非默认字符

数字梯度的意外值

如何通过多2多字段过滤查询集

将输入管道传输到正在运行的Python脚本中

什么相当于pytorch中的numpy累积ufunc

用Python解密Java加密文件

Python键入协议默认值

我如何使法国在 map 中完全透明的代码?

PyQt5,如何使每个对象的 colored颜色 不同?'

字符串合并语法在哪里记录

Python脚本使用蓝牙运行在Windows 11与raspberry pi4

启用/禁用shiny 的自动重新加载

判断solve_ivp中的事件

在Python中从嵌套的for循环中获取插值

为什么我的sundaram筛这么低效

Pandas—堆栈多索引头,但不包括第一列

使用tqdm的进度条

简单 torch 模型测试:ModuleNotFoundError:没有名为';Ultralytics.yolo';

如何训练每一个pandaprame行的线性回归并生成斜率

为罕见情况下的回退None值键入