How to drop rows of Pandas DataFrame whose value in a certain column is NaN

前端 未结 12 842
一生所求
一生所求 2020-11-22 00:59

I have this DataFrame and want only the records whose EPS column is not NaN:

>>> df
                 STK_ID           


        
相关标签:
12条回答
  • 2020-11-22 01:11

    Simplest of all solutions:

    filtered_df = df[df['EPS'].notnull()]
    

    The above solution is way better than using np.isfinite()

    0 讨论(0)
  • 2020-11-22 01:11

    Another version:

    df[~df['EPS'].isna()]
    
    0 讨论(0)
  • 2020-11-22 01:16

    You can use this:

    df.dropna(subset=['EPS'], how='all', inplace=True)
    
    0 讨论(0)
  • 2020-11-22 01:19

    It may be added at that '&' can be used to add additional conditions e.g.

    df = df[(df.EPS > 2.0) & (df.EPS <4.0)]
    

    Notice that when evaluating the statements, pandas needs parenthesis.

    0 讨论(0)
  • 2020-11-22 01:20

    Don't drop, just take the rows where EPS is not NA:

    df = df[df['EPS'].notna()]
    
    0 讨论(0)
  • 2020-11-22 01:21

    I know this has already been answered, but just for the sake of a purely pandas solution to this specific question as opposed to the general description from Aman (which was wonderful) and in case anyone else happens upon this:

    import pandas as pd
    df = df[pd.notnull(df['EPS'])]
    
    0 讨论(0)
提交回复
热议问题