从pandas数据帧中删除具有空值的行

42

我正在尝试从我的数据框中删除一行,其中一个列的值为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)

这是我的输出:

Output

请问有人能告诉我如何删除这一行,最好是通过识别空值来删除以及如何按日期删除?

我还没有太长时间使用pandas,我已经卡了一个小时了。任何建议都将不胜感激。

5个回答

57

这应该可以完成工作:

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

它将擦除每一行(轴=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

请参考文档以获取更多详细信息。

如果您的DataFrame一切正常,删除NaN值应该很容易。如果仍然无法正常工作,请确保为列定义了正确的数据类型(可以考虑使用pd.to_numeric...)


1
我的解决方法是在参数中包含“null” na_values(['NaN','null']) 这将传递给pandas.read_csv()以创建df。如果这种方法不可行,则仍然没有解决方案。 - ryan pickles

18

----清除所有列中的空值-------

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

请原谅任何错误。


1
这对我非常有效,谢谢。同时也适用于提取唯一的非空值..df [~df ['B'] .isnull()] .unique() - ZakS
df[df['B'].notnull()] - Begoodpy

5

要删除所有的空值,dropna() 方法会很有帮助。

df.dropna(inplace=True)

如果需要删除包含 null 值的特定数据,可以使用以下代码:

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

1

我建议尝试其中一行:

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

1

看起来您的列中的值是"null"而不是dropna要处理的真正NaN。所以我建议尝试:

df[df.BBD != 'null']

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

df[pd.notnull(df.BBD)]

这是唯一有效的解决方案。 - Nagesh Singh Chauhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接