【问题标题】:Remove row with null value from pandas data frame从熊猫数据框中删除具有空值的行
【发布时间】:2017-06-14 15:19:07
【问题描述】:

我正在尝试从我的数据框中删除一行,其中一列的值为 null。我能找到的大部分帮助都与删除到目前为止对我不起作用的 NaN 值有关。

我在这里创建了数据框:

  # successfully crated data frame
 df1 = ut.get_data(symbols, dates) # column heads are 'SPY', 'BBD'

# can't get rid of row containing null val in column BBD
# tried each of these with the others commented out but always had an 
# error or sometimes I was able to get a new column of boolean values
# but i just want to drop the row
df1 = pd.notnull(df1['BBD']) # drops rows with null val, not working
df1 = df1.drop(2010-05-04, axis=0)
df1 = df1[df1.'BBD' != null]
df1 = df1.dropna(subset=['BBD'])
df1 = pd.notnull(df1.BBD)


# I know the date to drop but still wasn't able to drop the row
df1.drop([2015-10-30])
df1.drop(['2015-10-30'])
df1.drop([2015-10-30], axis=0)
df1.drop(['2015-10-30'], axis=0)


with pd.option_context('display.max_row', None):
    print(df1)

这是我的输出:

有人可以告诉我如何删除这一行,最好是通过空值标识该行以及如何按日期删除?

我与 pandas 合作的时间不长,我已经坚持了一个小时。任何建议将不胜感激。

【问题讨论】:

    标签: python pandas dataframe null


    【解决方案1】:

    这应该可以完成工作:

    df = df.dropna(how='any',axis=0) 
    

    它将擦除其中包含“any”Null 值的每个 (axis=0)。

    示例:

    #Recreate random DataFrame with Nan values
    df = pd.DataFrame(index = pd.date_range('2017-01-01', '2017-01-10', freq='1d'))
    # Average speed in miles per hour
    df['A'] = np.random.randint(low=198, high=205, size=len(df.index))
    df['B'] = np.random.random(size=len(df.index))*2
    
    #Create dummy NaN value on 2 cells
    df.iloc[2,1]=None
    df.iloc[5,0]=None
    
    print(df)
                    A         B
    2017-01-01  203.0  1.175224
    2017-01-02  199.0  1.338474
    2017-01-03  198.0       NaN
    2017-01-04  198.0  0.652318
    2017-01-05  199.0  1.577577
    2017-01-06    NaN  0.234882
    2017-01-07  203.0  1.732908
    2017-01-08  204.0  1.473146
    2017-01-09  198.0  1.109261
    2017-01-10  202.0  1.745309
    
    #Delete row with dummy value
    df = df.dropna(how='any',axis=0)
    
    print(df)
    
                    A         B
    2017-01-01  203.0  1.175224
    2017-01-02  199.0  1.338474
    2017-01-04  198.0  0.652318
    2017-01-05  199.0  1.577577
    2017-01-07  203.0  1.732908
    2017-01-08  204.0  1.473146
    2017-01-09  198.0  1.109261
    2017-01-10  202.0  1.745309
    

    有关详细信息,请参阅reference

    如果您的 DataFrame 一切正常,那么删除 NaN 应该就这么简单。如果这仍然不起作用,请确保为您的列定义了正确的数据类型(想到pd.to_numeric...)

    【讨论】:

    • 我的解决方法是在参数 na_values(['NaN', 'null']) 中包含“null”,该参数被传递给 pandas.read_csv() 以创建 df。仍然没有解决方案,这是不可能的
    【解决方案2】:

    ----清空所有列-------

    df = df.dropna(how='any',axis=0)
    

    ---如果您想通过基于 1 列清除 NULL。---

    df[~df['B'].isnull()]
    

                    A         B
    2017-01-01  203.0  1.175224
    2017-01-02  199.0  1.338474
                                  **2017-01-03  198.0       NaN** clean
    2017-01-04  198.0  0.652318
    2017-01-05  199.0  1.577577
    2017-01-06    NaN  0.234882
    2017-01-07  203.0  1.732908
    2017-01-08  204.0  1.473146
    2017-01-09  198.0  1.109261
    2017-01-10  202.0  1.745309
    

    如有错误请见谅。

    【讨论】:

    • 这对我很有用,谢谢。也适用于提取唯一的非空值 ..df[~df['B'].isnull()].unique()
    【解决方案3】:

    删除所有空值 dropna() 方法会很有帮助

    df.dropna(inplace=True)
    

    要删除包含特定空值的删除,请使用此代码

    df.dropna(subset=['column_name_to_remove'], inplace=True)
    

    【讨论】:

      【解决方案4】:

      您的列中的值似乎是“null”,而不是真正的 NaN,这正是 dropna 的含义。所以我会尝试:

      df[df.BBD != 'null']
      

      或者,如果该值实际上是 NaN,那么,

      df[pd.notnull(df.BBD)]
      

      【讨论】:

        【解决方案5】:

        我建议尝试以下两行之一:

        df_clean = df1[df1['BBD'].isnull() == False]
        df_clean = df1[df1['BBD'].isna() == False]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-11-12
          • 1970-01-01
          • 2013-07-02
          • 1970-01-01
          • 1970-01-01
          • 2017-09-27
          • 1970-01-01
          相关资源
          最近更新 更多