iloc[[0]]返回DataFrame类型
iloc[0]返回Series类型
df=pd.DataFrame(array,index=[2,1,3,4],columns=list("abcdef"))
print(type(df.iloc[[0]]))
print(type(df.iloc[0]))
输出
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.series.Series'>
而在对DataFrame变量进行条件过滤时使用iloc[[0]]来判断会报错,如下:
print(df.loc[:,(df.iloc[[0]]>-10)&(df.iloc[[0]]<-8)])
输出:
ValueError: Cannot index with multidimensional key
改用iloc[0]得到正确结果
print(df.loc[:,(df.iloc[0]>-10)&(df.iloc[0]<-8)])
输出:
d
2 -9
1 -3
3 3
4 9