以下是否有不同之处?
dfCombined[col][dfCombined[col].isnull()]
dfCombined[col].loc[dfCombined[col].isnull()]
我检查了iloc
和ix
,它们不支持布尔选择?
答案 0 :(得分:0)
我认为如果您需要boolean indexing
过滤列col
,请使用loc
:
dfCombined.loc[dfCombined[col].isnull(), 'col']
如果需要省略所有列loc
:
dfCombined[dfCombined[col].isnull()]
样品:
dfCombined = pd.DataFrame({'col':[1,2,np.nan],
'col1':[4,5,6]})
print (dfCombined)
col col1
0 1.0 4
1 2.0 5
2 NaN 6
print (dfCombined.loc[dfCombined['col'].isnull(), 'col'])
2 NaN
Name: col, dtype: float64
#select second column (1)
print (dfCombined.ix[dfCombined['col'].isnull(), 1])
2 6
Name: col1, dtype: int64
print (dfCombined.iloc[dfCombined['col'].isnull(), 1])
NotImplementedError: iLocation based boolean indexing on an integer type is not available
print (dfCombined[dfCombined['col'].isnull()])
col col1
2 NaN 6
问题:
适用于两种方法,但更优先选择ix
和loc
来选择列 - 请参阅cookbook。
print (dfCombined['col'][dfCombined['col'].isnull()])
2 NaN
Name: col, dtype: float64
print (dfCombined['col'].loc[dfCombined['col'].isnull()])
2 NaN
Name: col, dtype: float64
答案 1 :(得分:-1)
在熊猫中,df.iloc
仅适用于布尔值
示例:
df.iloc[df[‘col’]==False/True]
其中df.loc
适用于非布尔值
示例:
df.loc[df[‘col’]==values]