df.dropna()函数用于删除dataframe数据中的缺失数据,即 删除nan数据.
官方函数说明:
1
2
3
4
5
6
7
|
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. |
参数说明:
parameters | 说明 |
---|---|
axis | 0为行 1为列,default 0,数据删除维度 |
how | {‘any', ‘all'}, default ‘any',any:删除带有nan的行;all:删除全为nan的行 |
thresh | int,保留至少 int 个非nan行 |
subset | list,在特定列缺失值处理 |
inplace | bool,是否修改源文件 |
测试:
1
2
3
4
|
>>>df = pd.dataframe({ "name" : [ 'alfred' , 'batman' , 'catwoman' ], "toy" : [np.nan, 'batmobile' , 'bullwhip' ], "born" : [pd.nat, pd.timestamp( "1940-04-25" ), pd.nat]}) |
1
2
3
4
5
|
>>>df name toy born 0 alfred nan nat 1 batman batmobile 1940 - 04 - 25 2 catwoman bullwhip nat |
删除至少缺少一个元素的行:
1
2
3
|
>>>df.dropna() name toy born 1 batman batmobile 1940 - 04 - 25 |
删除至少缺少一个元素的列:
1
2
3
4
5
|
>>>df.dropna(axis = 1 ) name 0 alfred 1 batman 2 catwoman |
删除所有元素丢失的行:
1
2
3
4
5
|
>>>df.dropna(how = 'all' ) name toy born 0 alfred nan nat 1 batman batmobile 1940 - 04 - 25 2 catwoman bullwhip nat |
只保留至少2个非na值的行:
1
2
3
4
|
>>>df.dropna(thresh = 2 ) name toy born 1 batman batmobile 1940 - 04 - 25 2 catwoman bullwhip nat |
从特定列中查找缺少的值:
1
2
3
|
>>>df.dropna(subset = [ 'name' , 'born' ]) name toy born 1 batman batmobile 1940 - 04 - 25 |
修改原数据:
1
2
3
4
|
>>>df.dropna(inplace = true) >>>df name toy born 1 batman batmobile 1940 - 04 - 25 |
以上。
补充:pandas 之dropna滤除缺失数据
约定:
1
2
3
|
import pandas as pd import numpy as np from numpy import nan as nan |
滤除缺失数据
pandas的设计目标之一就是使得处理缺失数据的任务更加轻松些。pandas使用nan作为缺失数据的标记。
使用dropna使得滤除缺失数据更加得心应手。
一、处理series对象
通过**dropna()**滤除缺失数据:
1
2
3
|
se1 = pd.series([ 4 ,nan, 8 ,nan, 5 ]) print (se1) se1.dropna() |
代码结果:
1
2
3
4
5
6
7
8
9
10
|
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 |
通过布尔序列也能滤除:
1
|
se1[se1.notnull()] |
代码结果:
1
2
3
4
|
0 4.0 2 8.0 4 5.0 dtype: float64 |
二、处理dataframe对象
处理dataframe对象比较复杂,因为你可能需要丢弃所有的nan或部分nan。
1
2
|
df1 = pd.dataframe([[ 1 , 2 , 3 ],[nan,nan, 2 ],[nan,nan,nan],[ 8 , 8 ,nan]]) df1 |
代码结果:
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 |
默认滤除所有包含nan:
1
|
df1.dropna() |
代码结果:
0 | 1 | 2 | |
---|---|---|---|
0 | 1.0 | 2.0 | 3.0 |
传入**how=‘all'**滤除全为nan的行:
1
|
df1.dropna(how = 'all' ) |
代码结果:
0 | 1 | 2 | |
---|---|---|---|
0 | 1.0 | 2.0 | 3.0 |
1 | nan | nan | 2.0 |
3 | 8.0 | 8.0 | nan |
传入axis=1滤除列:
1
2
|
df1[ 3 ] = nan df1 |
代码结果:
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 |
1
|
df1.dropna(axis = 1 ,how = "all" ) |
代码结果:
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个非nan数据的行:
1
|
df1.dropna(thresh = 1 ) |
代码结果:
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 |
1
|
df1.dropna(thresh = 3 ) |
代码结果:
0 | 1 | 2 | 3 | |
---|---|---|---|---|
0 | 1.0 | 2.0 | 3.0 | nan |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/qq_43188358/article/details/108335776