Python pandas.replace的用法详解

目录

1. pandas.replace()介绍

pandas.Series.replace 官方文档

Series.replace(to_replace=None, value=NoDefault.no_default, inplace=False, limit=None, regex=False, method=NoDefault.no_default)

  • to_replace: 需要替换的值
  • value:替换后的值
  • inplace: 是否在原数据表上更改,默认 inplace=False
  • limit:向前或向后填充的最大尺寸间隙,用于填充缺失值
  • regex: 是否模糊查询,用于正则表达式查找,默认 regex=False
  • method: 填充方式,用于填充缺失值(The method to use when for replacement, when to_replace is a scalar, list or tuple and value is None.)
    • pad: 向前填充
    • ffill: 向前填充
    • bfill: 向后填充

Example

Python pandas.replace的用法详解

2. 单值替换

2.1 全局替换

df.replace(1, 10)

Python pandas.replace的用法详解

2.2 选定条件替换

df[\'attr_1\'].replace(\'场景.季节.冬天\', \'冬天\', inplace=True)

Python pandas.replace的用法详解

3. 多值替换

3.1 多个值替换同一个值

df.replace([3, 11, 137], 4)

Python pandas.replace的用法详解

3.2 多个值替换不同值

列表List

df.replace([3, 11, 137, 1], [1, 111, 731, 10])

Python pandas.replace的用法详解

字典映射

# 修改不同列
df.replace({\'场景.普通运动.跑步\':\'跑步\', 11:100})

Python pandas.replace的用法详解

# 修改同一列
df.replace({\'attr_1\':{\'场景.普通运动.跑步\':\'跑步\', \'场景.户外休闲.爬山\':\'爬山\'}})

Python pandas.replace的用法详解

4. 模糊查询替换

df.replace(\'场景.\',\'\', regex=True)
df.replace(regex=\'场景.\', value=\' \')

Python pandas.replace的用法详解

df.replace(regex={\'场景.\': \'\', \'方案.\':\'\'})
df.replace(regex=[\'场景.\', \'方案.\'], value=\'\')

Python pandas.replace的用法详解

也可以这样

df[\'Attr_B\'] = df[\'Attr_B\'].str.replace(\'夹克\', \'大衣\')
df

Python pandas.replace的用法详解

5. 缺失值替换

5.1 method的用法 (向前/后填充)

Example

Python pandas.replace的用法详解

向前填充(以他的前一行的值填充)

s.replace(np.nan, method=\'pad\')
s.replace(np.nan, method=\'ffill\')

Python pandas.replace的用法详解

向后填充(以他的后一行的值填充)

s.replace(np.nan, method=\'bfill\')

Python pandas.replace的用法详解

5.2 limit的用法 (限制最大填充间隔)

连着多个空值时,limit为几填充几个

Example

Python pandas.replace的用法详解

s.replace(np.nan, method=\'ffill\', limit=1)

Python pandas.replace的用法详解

s.replace(np.nan, method=\'ffill\', limit=2)

Python pandas.replace的用法详解

补充:使用实例代码

#Series对象值替换
s = df.iloc[2]#获取行索引为2数据
#单值替换
s.replace(\'?\',np.nan)#用np.nan替换?
s.replace({\'?\':\'NA\'})#用NA替换?
#多值替换
s.replace([\'?\',r\'$\'],[np.nan,\'NA\'])#列表值替换
s.replace({\'?\':np.nan,\'$\':\'NA\'})#字典映射
#同缺失值填充方法类似
s.replace([\'?\',\'$\'],method=\'pad\')#向前填充
s.replace([\'?\',\'$\'],method=\'ffill\')#向前填充
s.replace([\'?\',\'$\'],method=\'bfill\')#向后填充
#limit参数控制填充次数
s.replace([\'?\',\'$\'],method=\'bfill\',limit=1)
#DataFrame对象值替换
#单值替换
df.replace(\'?\',np.nan)#用np.nan替换?
df.replace({\'?\':\'NA\'})#用NA替换?
#按列指定单值替换
df.replace({\'EMPNO\':\'?\'},np.nan)#用np.nan替换EMPNO列中?
df.replace({\'EMPNO\':\'?\',\'ENAME\':\'.\'},np.nan)#用np.nan替换EMPNO列中?和ENAME中.
#多值替换
df.replace([\'?\',\'.\',\'$\'],[np.nan,\'NA\',\'None\'])##用np.nan替换?用NA替换. 用None替换$
df.replace({\'?\':\'NA\',\'$\':None})#用NA替换? 用None替换$
df.replace({\'?\',\'$\'},{\'NA\',None})#用NA替换? 用None替换$
#正则替换
df.replace(r\'\\?|\\.|\\$\',np.nan,regex=True)#用np.nan替换?或.或$原字符
df.replace([r\'\\?\',r\'\\$\'],np.nan,regex=True)#用np.nan替换?和$
df.replace([r\'\\?\',r\'\\$\'],[np.nan,\'NA\'],regex=True)#用np.nan替换?用NA替换$符号
df.replace(regex={r\'\\?\':None})
#value参数显示传递
df.replace(regex=[r\'\\?|\\.|\\$\'],value=np.nan)#用np.nan替换?或.或$原字符

总结 

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容