Pandas 的 DataFrame.replace(~) 方法用另一组值替换指定的值。
参数
1.to_replace | string或regex或list或dict或Series或number或None
将被替换的值。
2. value | number 或 dict 或 list 或 string 或 regex 或 None | optional
将替换 to_replace 的值。默认情况下,value=None 。
3. inplace | boolean | optional
-
如果是
True,那么该方法将直接修改源DataFrame,而不是创建新的DataFrame。 -
如果是
False,则将创建并返回一个新的DataFrame。
默认情况下,inplace=False 。
4. limit | int | optional
要执行的连续填充的最大数量。默认情况下,limit=None 。
5. regex | boolean 或 string | optional
如果 True ,则 to_replace 被解释为正则表达式。请注意,这要求 to_replace 是一个字符串。
默认情况下,regex=False 。
6. method | string 或 None | optional
替换 to_replace 的规则:
|
方法 |
说明 |
|---|---|
|
|
使用前一行的值填充该值。 |
|
|
使用下一行的值填充该值。 |
该参数仅在 value=None 时生效。默认情况下,method="pad" 。
返回值
DataFrame,其中指定值替换为您所需的值。
例子
用单个值替换单个值
考虑以下 DataFrame :
df = pd.DataFrame({"A":[1,2], "B":[3,4]})
df
A B
0 1 3
1 2 4
要将 1 的所有值替换为 5 :
df.replace(1, 5)
A B
0 5 3
1 2 4
用单个值替换多个值
考虑以下 DataFrame :
df = pd.DataFrame({"A":[1,2], "B":[3,4]})
df
A B
0 1 3
1 2 4
要将 1 和 2 的所有值替换为 5 :
df.replace([1,2], 5)
A B
0 5 3
1 5 4
将多个值替换为相应的值
使用数组
考虑以下 DataFrame :
df = pd.DataFrame({"A":[1,2], "B":[3,4]})
df
A B
0 1 3
1 2 4
分别用 5 和 6 替换 1 和 2 的所有值:
df.replace([1,2], [5,6])
A B
0 5 3
1 6 4
使用字典
考虑以下 DataFrame :
df = pd.DataFrame({"A":[1,2], "B":[3,4]})
df
A B
0 1 3
1 2 4
分别用 5 和 6 替换 1 和 3 的所有值:
df.replace({1:5, 3:6})
A B
0 5 6
1 2 4
使用正则表达式替换
考虑以下 DataFrame :
df = pd.DataFrame({"A":["alex","bob"], "B":["cathy","doge"]})
df
A B
0 alex cathy
1 bob doge
要将所有以字母 "a" 开头的值替换为 "eric" :
df.replace("^a.*", "eric", regex=True)
A B
0 eric cathy
1 bob doge
请注意我们如何通过指定 regex=True 来启用正则表达式。
仅替换某些列
用单个值替换单个值
考虑以下 DataFrame :
df = pd.DataFrame({"A":[1,2], "B":[1,2]})
df
A B
0 1 1
1 2 2
仅将 A 列的 1 的所有值替换为 3 。为此,我们必须提供 dict ,如下所示:
df.replace({"A":1}, 3)
A B
0 3 1
1 2 2
请注意,尽管 B 列包含值 1 ,但它并未受到影响。
将多个值替换为相应的值
考虑以下 DataFrame :
df = pd.DataFrame({"A":[1,2], "B":[3,4]})
df
A B
0 1 3
1 2 4
要将 1 和 2 分别替换为 5 和 6 列 A :
df.replace({"A":{1:5, 2:6}})
A B
0 5 3
1 6 4
使用填充替换
当您没有显式提供value参数时,replace(~)函数将自动前向填充匹配的值,即将to_replace替换为前一行的值。
考虑以下 DataFrame :
df = pd.DataFrame({"A":["a","b","c"]})
df
A
0 a
1 b
2 c
前向填充
要前向填充所有出现的 "b" :
df.replace("b", method="ffill") # or simply leave out the method parameter.
A
0 a
1 a
2 c
请注意值 a (匹配项之前的值(即 "b" ))如何用作填充符。
警告
当没有前一行时,不会替换任何内容。
考虑当我们想要前向填充所有出现的 "a" 的情况:
df.replace("a", method="ffill")
A
0 a
1 b
2 c
请注意,即使我们的 DataFrame 中有"a",它也没有被替换。由于没有前一行,因此我们没有填充符,因此不执行替换。
向后填充
要向后填充所有出现的 "b" :
df.replace("b", method="bfill")
A
0 a
1 c
2 c
请注意匹配后紧随其后的值 "c" (即 "b" )如何用作填充符。
警告
当没有下一行时,不会替换任何内容。
考虑当我们想要向后填充所有出现的 "c" 时的情况:
df.replace("c", method="bfill")
A
0 a
1 b
2 c
请注意,即使我们的 DataFrame 中有"c",它也没有被替换。由于没有下一行,因此我们没有填充符,因此不执行替换。
限制
考虑以下 DataFrame :
df = pd.DataFrame({"A":["a","b","b"]})
df
A
0 a
1 b
2 b
默认情况下, limit=None ,这意味着允许连续填充的次数没有限制:
df.replace("b", method="ffill")
A
0 a
1 a
2 a
相反,设置 limit=1 会产生:
df.replace("b", method="ffill", limit=1)
A
0 a
1 a
2 b
在这里,注意如何b只被填满一次。另请注意, limit 对连续的仅填充。
就地更换
要执行就地替换,我们需要设置 inplace=True 。这将直接对源 DataFrame 执行替换操作,而不是创建新的。
考虑以下 DataFrame :
df = pd.DataFrame({"A":[1,2],"B":[3,4]})
df
A B
0 1 3
1 2 4
我们用 inplace=True 替换所有出现的 1 和 5 :
df.replace(1, 5, inplace=True)
df
A B
0 5 3
1 2 4
如输出所示,源DataFrame已被直接修改。
相关用法
- Python PySpark DataFrame replace方法用法及代码示例
- Python PySpark DataFrame repartition方法用法及代码示例
- Python Pandas DataFrame reset_index方法用法及代码示例
- Python Pandas DataFrame reorder_levels方法用法及代码示例
- Python Pandas DataFrame resample方法用法及代码示例
- Python Pandas DataFrame reindex方法用法及代码示例
- Python Pandas DataFrame rename_axis方法用法及代码示例
- Python Pandas DataFrame rename方法用法及代码示例
- Python Pandas DataFrame rank方法用法及代码示例
- Python Pandas DataFrame rdiv方法用法及代码示例
- Python Pandas DataFrame radd方法用法及代码示例
- Python PySpark DataFrame rdd属性用法及代码示例
- Python Pandas DataFrame rsub方法用法及代码示例
- Python Pandas DataFrame round方法用法及代码示例
- Python PySpark DataFrame randomSplit方法用法及代码示例
- Python Pandas DataFrame rolling方法用法及代码示例
- Python Pandas DataFrame rpow方法用法及代码示例
- Python Pandas DataFrame rfloordiv方法用法及代码示例
- Python Pandas DataFrame rtruediv方法用法及代码示例
- Python Pandas DataFrame rmod方法用法及代码示例
- Python Pandas DataFrame rmul方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | replace method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
