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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
