【Python 04】数据清洗:fillna()处理数据行中的缺失/异常值

图片一

图片二

对比图片二与图片一目的是:使各个 INDEX 后面6列数据等于其不为空值(not nan)且不为”.”的值。
那处理方式就大概就会有几种了,其中一种处理思路:
把不同的 INDEX 里的第一条中的”.”和空值全部换成其下面的数据。
所以就会用到pandas里的 fillna() 或者 bfill() or ffill() 函数来处理。

import numpy as np
import pandas as pd
df = pd.read_excel(r"C:\xxxx\xxxx\xxxx\test list.xlsx")

图片三


df=df.replace('.',np.NAN)

df=df.bfill()

df=df.groupby('INDEX').first()

一、用空值替换掉”.”

二、用bfill向上填充

三、按INDEX 分组后取第一条数据

OK了,这样的话达到了我们预期目标啦~
我们来看看这题目所涉及的知识点 knowledge point 吧~

Pandas.DataFrame.fillna
Pandas 官方文档:
填充”空值”用的是这个特别的函数方式。
参数(因素)包括:
DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)

  • value: scalar, dict, Series, or DataFrame
    支持的数据类型:scalar(标量), dict, Series, or DataFrame
    Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). Values not in the dict/Series/DataFrame will not be filled. This value cannot be a list.

value 是用于填充的空值的值。
* method: {‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}, default None
Method to use for filling holes in reindexed Series pad / ffill: propagate last valid observation forward to next valid backfill / bfill: use next valid observation to fill gap.

method参数:改变替代值的方式,当为’ffill’,表示用前面的值填充,当’bfill’表示用后面的值填充。
* axis: {0 or ‘index’, 1 or ‘columns’}
Axis along which to fill missing values.

axis参数默认为0,即沿着行填充,为1则沿着列填充
* inplace: bool, default False
If True, fill in-place. Note: this will modify any other views on this object (e.g., a no-copy slice for a column in a DataFrame).

传入inplace参数:是否在原来的数据上操作,默认为False,表示重新拷贝了一份数据,然后在拷贝的数据上操作。
* limit: int, default None
If method is specified, this is the maximum number of consecutive NaN values to forward/backward fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. If method is not specified, this is the maximum number of entries along the entire axis where NaNs will be filled. Must be greater than 0 if not None.

传入limit=” “限制填充个数
* downcast: dict, default is None
A dict of item->dtype of what to downcast if possible, or the string ‘infer’ which will try to downcast to an appropriate equal type (e.g. float64 to int64 if possible).

Original: https://blog.csdn.net/eason_nnn/article/details/123233093
Author: Eason DayDayUp
Title: 【Python 04】数据清洗:fillna()处理数据行中的缺失/异常值

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/738494/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球