校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識(shí)庫 > pandas || df.dropna() 缺失值刪除操作

pandas || df.dropna() 缺失值刪除操作

熱門標(biāo)簽:清遠(yuǎn)360地圖標(biāo)注方法 原裝電話機(jī)器人 平頂山外呼系統(tǒng)免費(fèi) 工廠智能電話機(jī)器人 江蘇客服外呼系統(tǒng)廠家 西藏智能外呼系統(tǒng)五星服務(wù) 在哪里辦理400電話號(hào)碼 400電話申請(qǐng)服務(wù)商選什么 千陽自動(dòng)外呼系統(tǒng)

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,是否修改源文件

測(cè)試:

>>>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對(duì)象

通過**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對(duì)象

處理DataFrame對(duì)象比較復(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ò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • 詳解Pandas 處理缺失值指令大全
  • Pandas缺失值2種處理方式代碼實(shí)例
  • pandas中read_csv的缺失值處理方式
  • 簡(jiǎn)單了解Pandas缺失值處理方法
  • pandas 缺失值與空值處理的實(shí)現(xiàn)方法

標(biāo)簽:日照 西安 隨州 股票 白城 安慶 錦州 天水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pandas || df.dropna() 缺失值刪除操作》,本文關(guān)鍵詞  pandas,df.dropna,缺失,值,刪除,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《pandas || df.dropna() 缺失值刪除操作》相關(guān)的同類信息!
  • 本頁收集關(guān)于pandas || df.dropna() 缺失值刪除操作的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 鄯善县| 大洼县| 肥东县| 阿尔山市| 祥云县| 农安县| 勐海县| 和顺县| 资讯 | 皮山县| 杨浦区| 遂平县| 禹城市| 通道| 武鸣县| 明水县| 伊宁县| 苍山县| 沽源县| 顺平县| 塔城市| 许昌县| 宣化县| 常宁市| 和田县| 财经| 岫岩| 基隆市| 通辽市| 商都县| 肃北| 乡城县| 北碚区| 临清市| 长子县| 富宁县| 阳信县| 博罗县| 南和县| 济阳县| 乐亭县|