整数类型上基于 iLocation 的布尔索引不可用

Ank*_*ngh 7 pandas

我有一个问题,我想获取那些包含缺失值的行。pd.isnull对表中的“里程”列使用 iloc 和。

import pandas as pd
df=pd.read_csv('BikeList.csv')
d1=df['Mileage']
print(d1)
print(pd.isnull(df['Mileage']))
d2=df.iloc[pd.isnull(df['Mileage']),['Bike','Mileage']]
Run Code Online (Sandbox Code Playgroud)

我有这个错误,

iLocation based boolean indexing on an integer type is not available
Run Code Online (Sandbox Code Playgroud)

import pandas as pd
df=pd.read_csv('BikeList.csv')
d1=df['Mileage']
print(d1)
print(pd.isnull(df['Mileage']))
d2=df.iloc[pd.isnull(df['Mileage']),['Bike','Mileage']]
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 11

您需要使用DataFrame.loc,因为按标签选择BikeMileage

d2 = df.loc[pd.isnull(df['Mileage']),['Bike','Mileage']]
Run Code Online (Sandbox Code Playgroud)

或使用Series.isna

d2 = df.loc[df['Mileage'].isna(),['Bike','Mileage']]
Run Code Online (Sandbox Code Playgroud)

如果需要DataFrame.iloc,将布尔掩码转换为 numpy 数组,但也将列转换为列的位置Index.get_indexer

d2 = df.iloc[pd.isnull(df['Mileage']).values, df.columns.get_indexer(['Bike','Mileage'])]
Run Code Online (Sandbox Code Playgroud)

样品

df = pd.DataFrame({
        'A':list('abcdef'),
         'Mileage':[np.nan,5,4,5,5,np.nan],
         'Bike':[7,8,9,4,2,3],
         'D':[1,3,5,7,1,0],
         'E':[5,3,6,9,2,4],
         'F':list('aaabbb')
})

print (df)
   A  Mileage  Bike  D  E  F
0  a      NaN     7  1  5  a
1  b      5.0     8  3  3  a
2  c      4.0     9  5  6  a
3  d      5.0     4  7  9  b
4  e      5.0     2  1  2  b
5  f      NaN     3  0  4  b

d2 = df.loc[pd.isnull(df['Mileage']),['Bike','Mileage']]
print (d2)
   Bike  Mileage
0     7      NaN
5     3      NaN

d2 = df.iloc[pd.isnull(df['Mileage']).values, df.columns.get_indexer(['Bike','Mileage'])]
print (d2)
   Bike  Mileage
0     7      NaN
5     3      NaN
Run Code Online (Sandbox Code Playgroud)