且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-17 19:57:48

这应该可以完成工作:

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