从 pandas 数据框中删除具有空值的行 [英] Remove row with null value from pandas data frame

查看:111
本文介绍了从 pandas 数据框中删除具有空值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据框中删除一行中其中某一列的值为null的行.我能找到的大多数帮助都与删除NaN值有关,到目前为止,该值对我没有用.

I'm trying to remove a row from my data frame in which one of the columns has a value of null. Most of the help I can find relates to removing NaN values which hasn't worked for me so far.

我在这里创建了数据框:

Here I've created the data frame:

  # 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)

这是我的输出:

有人可以告诉我如何删除该行,最好同时用空值标识该行和如何按日期删除?

Can someone please tell me how I can drop this row, preferably both by identifying the row by the null value and how to drop by date?

我没有和熊猫合作很久了,我已经坚持了一个小时.任何建议将不胜感激.

I haven't been working with pandas very long and I've been stuck on this for an hour. Any advice would be much appreciated.

推荐答案

这应该可以完成工作:

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

它将删除其中具有"任何"空值的每个(轴= 0).

It will erase every row (axis=0) that has "any" Null value in it.

示例:

#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

有关详细信息,请参见参考

See the reference for further detail.

如果您的DataFrame一切正常,那么删除NaN应该很容易.如果仍然无法解决问题,请确保为列定义了正确的数据类型( pd.to_numeric 浮现在脑海中...)

If everything is OK with your DataFrame, dropping NaNs should be as easy as that. If this is still not working, make sure you have the proper datatypes defined for your column (pd.to_numeric comes to mind...)

这篇关于从 pandas 数据框中删除具有空值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆