pandas || df.dropna() 缺失值刪除操作
df.dropna()函數(shù)用于刪除dataframe數(shù)據(jù)中的缺失數(shù)據(jù),即 刪除NaN數(shù)據(jù).
官方函數(shù)說明:
DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False) Remove missing values. See the User Guide for more on which values are considered missing, and how to work with missing data. Returns DataFrame DataFrame with NA entries dropped from it.
參數(shù)說明:
Parameters | 說明 |
---|---|
axis | 0為行 1為列,default 0,數(shù)據(jù)刪除維度 |
how | {‘a(chǎn)ny', ‘a(chǎn)ll'}, default ‘a(chǎn)ny',any:刪除帶有nan的行;all:刪除全為nan的行 |
thresh | int,保留至少 int 個(gè)非nan行 |
subset | list,在特定列缺失值處理 |
inplace | bool,是否修改源文件 |
測試:
>>>df = pd.DataFrame({"name": ['Alfred', 'Batman', 'Catwoman'], "toy": [np.nan, 'Batmobile', 'Bullwhip'], "born": [pd.NaT, pd.Timestamp("1940-04-25"), pd.NaT]})
>>>df name toy born 0 Alfred NaN NaT 1 Batman Batmobile 1940-04-25 2 Catwoman Bullwhip NaT
刪除至少缺少一個(gè)元素的行:
>>>df.dropna() name toy born 1 Batman Batmobile 1940-04-25
刪除至少缺少一個(gè)元素的列:
>>>df.dropna(axis=1) name 0 Alfred 1 Batman 2 Catwoman
刪除所有元素丟失的行:
>>>df.dropna(how='all') name toy born 0 Alfred NaN NaT 1 Batman Batmobile 1940-04-25 2 Catwoman Bullwhip NaT
只保留至少2個(gè)非NA值的行:
>>>df.dropna(thresh=2) name toy born 1 Batman Batmobile 1940-04-25 2 Catwoman Bullwhip NaT
從特定列中查找缺少的值:
>>>df.dropna(subset=['name', 'born']) name toy born 1 Batman Batmobile 1940-04-25
修改原數(shù)據(jù):
>>>df.dropna(inplace=True) >>>df name toy born 1 Batman Batmobile 1940-04-25
以上。
補(bǔ)充:Pandas 之Dropna濾除缺失數(shù)據(jù)
約定:
import pandas as pd import numpy as np from numpy import nan as NaN
濾除缺失數(shù)據(jù)
pandas的設(shè)計(jì)目標(biāo)之一就是使得處理缺失數(shù)據(jù)的任務(wù)更加輕松些。pandas使用NaN作為缺失數(shù)據(jù)的標(biāo)記。
使用dropna使得濾除缺失數(shù)據(jù)更加得心應(yīng)手。
一、處理Series對象
通過**dropna()**濾除缺失數(shù)據(jù):
se1=pd.Series([4,NaN,8,NaN,5]) print(se1) se1.dropna()
代碼結(jié)果:
0 4.0 1 NaN 2 8.0 3 NaN 4 5.0 dtype: float64 0 4.0 2 8.0 4 5.0 dtype: float64
通過布爾序列也能濾除:
se1[se1.notnull()]
代碼結(jié)果:
0 4.0 2 8.0 4 5.0 dtype: float64
二、處理DataFrame對象
處理DataFrame對象比較復(fù)雜,因?yàn)槟憧赡苄枰獊G棄所有的NaN或部分NaN。
df1=pd.DataFrame([[1,2,3],[NaN,NaN,2],[NaN,NaN,NaN],[8,8,NaN]]) df1
代碼結(jié)果:
0 | 1 | 2 | |
---|---|---|---|
0 | 1.0 | 2.0 | 3.0 |
1 | NaN | NaN | 2.0 |
2 | NaN | NaN | NaN |
3 | 8.0 | 8.0 | NaN |
默認(rèn)濾除所有包含NaN:
df1.dropna()
代碼結(jié)果:
0 | 1 | 2 | |
---|---|---|---|
0 | 1.0 | 2.0 | 3.0 |
傳入**how=‘a(chǎn)ll'**濾除全為NaN的行:
df1.dropna(how='all')
代碼結(jié)果:
0 | 1 | 2 | |
---|---|---|---|
0 | 1.0 | 2.0 | 3.0 |
1 | NaN | NaN | 2.0 |
3 | 8.0 | 8.0 | NaN |
傳入axis=1濾除列:
df1[3]=NaN df1
代碼結(jié)果:
0 | 1 | 2 | 3 | |
---|---|---|---|---|
0 | 1.0 | 2.0 | 3.0 | NaN |
1 | NaN | NaN | 2.0 | NaN |
2 | NaN | NaN | NaN | NaN |
3 | 8.0 | 8.0 | NaN | NaN |
df1.dropna(axis=1,how="all")
代碼結(jié)果:
0 | 1 | 2 | |
---|---|---|---|
0 | 1.0 | 2.0 | 3.0 |
1 | NaN | NaN | 2.0 |
2 | NaN | NaN | NaN |
3 | 8.0 | 8.0 | NaN |
傳入thresh=n保留至少有n個(gè)非NaN數(shù)據(jù)的行:
df1.dropna(thresh=1)
代碼結(jié)果:
0 | 1 | 2 | 3 | |
---|---|---|---|---|
0 | 1.0 | 2.0 | 3.0 | NaN |
1 | NaN | NaN | 2.0 | NaN |
3 | 8.0 | 8.0 | NaN | NaN |
df1.dropna(thresh=3)
代碼結(jié)果:
0 | 1 | 2 | 3 | |
---|---|---|---|---|
0 | 1.0 | 2.0 | 3.0 | NaN |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
用python 批量更改圖像尺寸到統(tǒng)一大小的方法
下面小編就為大家分享一篇用python 批量更改圖像尺寸到統(tǒng)一大小的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03python 發(fā)送和接收ActiveMQ消息的實(shí)例
今天小編就為大家分享一篇python 發(fā)送和接收ActiveMQ消息的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python argparse模塊實(shí)現(xiàn)解析命令行參數(shù)方法詳解
argparse 是 python 自帶的命令行參數(shù)解析包,可以用來方便的服務(wù)命令行參數(shù)。本文將通過示例和大家詳細(xì)講講argparse的使用,需要的可以參考一下2022-09-09python語言中print中加號、減號、乘號的應(yīng)用方式
這篇文章主要介紹了python語言中print中加號、減號、乘號的應(yīng)用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02