给定一个带有"BoolCol"列的DataFrame,我们想要找到DataFrame的索引,其中"BoolCol"的值== True
我目前有迭代的方式来做到这一点,它完美地工作:
for i in range(100,3000):
if df.iloc[i]['BoolCol']== True:
print i,df.iloc[i]['BoolCol']
Run Code Online (Sandbox Code Playgroud)
但这不是正确的熊猫方式.经过一些研究,我目前正在使用此代码:
df[df['BoolCol'] == True].index.tolist()
Run Code Online (Sandbox Code Playgroud)
这个给了我一个索引列表,但是当我通过执行以下操作检查它们时它们不匹配:
df.iloc[i]['BoolCol']
Run Code Online (Sandbox Code Playgroud)
结果实际上是假的!!
这是正确的熊猫方式吗?
这是否可以获得Seires的第一个元素而没有索引信息.
例如,我们有一个系列
import pandas as pd
key='MCS096'
SUBJECTS=pd.DataFrame({'ID':Series([146],index=[145]),\
'study':Series(['MCS'],index=[145]),\
'center':Series(['Mag'],index=[145]),\
'initials':Series(['MCS096'],index=[145])
})
Run Code Online (Sandbox Code Playgroud)
打印出SUBJECTS:
print (SUBJECTS[SUBJECTS.initials==key]['ID'])
145 146
Name: ID, dtype: int64
Run Code Online (Sandbox Code Playgroud)
如何在不使用索引145的情况下获取值146?
非常感谢你