脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python pandas.replace的用法详解

Python pandas.replace的用法详解

2022-06-24 14:49羊羊猪 Python

在处理数据的时候,很多时候会遇到批量替换的情况,如果一个一个去修改效率过低,也容易出错,replace()是很好的方法,下面这篇文章主要给大家介绍了关于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替换?或.或$原字符

 

总结 

到此这篇关于Python pandas.replace用法的文章就介绍到这了,更多相关Python pandas.replace用法内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_46599926/article/details/122846157

延伸 · 阅读

精彩推荐