iLocation based boolean indexing on an integer type is not available

后端 未结 1 1192
长发绾君心
长发绾君心 2021-01-19 16:26

I have an issue, i Want to get those rows which contains missing values. Using iloc and pd.isnull, for column \'Mileage\' in my table.

import p         


        
相关标签:
1条回答
  • 2021-01-19 17:02

    You need use DataFrame.loc, because select by labels Bike and Mileage:

    d2 = df.loc[pd.isnull(df['Mileage']),['Bike','Mileage']]
    

    Or use Series.isna:

    d2 = df.loc[df['Mileage'].isna(),['Bike','Mileage']]
    

    If need DataFrame.iloc is necessary convert boolean mask to numpy array, but also columns to positions of columns by Index.get_indexer:

    d2 = df.iloc[pd.isnull(df['Mileage']).values, df.columns.get_indexer(['Bike','Mileage'])]
    

    Sample:

    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
    
    0 讨论(0)
提交回复
热议问题