用法:
DataFrame.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method=None)將
to_replace中給出的值替換為value。- to_replace:數字、str 或 list-like
 要替換的值。
- 數字或字符串:
 - 等於 
to_replace的值將替換為value 
- 等於 
 
- 數字或字符串列表:
 - 如果
value也是list-like,則to_replace和value的長度必須相同。 
- 如果
 
- 字典:
 - 字典可用於為不同的現有值指定不同的替換值。例如,{‘a’: ‘b’, ‘y’: ‘z’} 將值 ‘a’ 替換為 ‘b’ and ‘y’ 和 ‘z’。要以這種方式使用字典,
value參數應該是None。 
- 字典可用於為不同的現有值指定不同的替換值。例如,{‘a’: ‘b’, ‘y’: ‘z’} 將值 ‘a’ 替換為 ‘b’ and ‘y’ 和 ‘z’。要以這種方式使用字典,
 
- value:標量,字典,list-like,str,默認無
 用於替換與
to_replace匹配的任何值的值。- inplace:布爾值,默認為 False
 如果為真,則到位。
- result:Series
 更換後的係列。掩碼和索引被保留。
- TypeError
 - 如果 
to_replace不是標量,則 array-like、dict 或 None - 如果
to_replace是一個字典並且值不是一個列表、字典或係列 
- 如果 
 - ValueError
 - 如果將列表傳遞給 
to_replace和value但它們的長度不同。 
- 如果將列表傳遞給 
 
參數:
返回:
拋出:
注意:
目前不支持的參數有:
limit,regex,method例子:
Series
標量
to_replace和value>>> import cudf >>> s = cudf.Series([0, 1, 2, 3, 4]) >>> s 0 0 1 1 2 2 3 3 4 4 dtype: int64 >>> s.replace(0, 5) 0 5 1 1 2 2 3 3 4 4 dtype: int64List-like
to_replace>>> s.replace([1, 2], 10) 0 0 1 10 2 10 3 3 4 4 dtype: int64dict-like
to_replace>>> s.replace({1:5, 3:50}) 0 0 1 5 2 2 3 50 4 4 dtype: int64 >>> s = cudf.Series(['b', 'a', 'a', 'b', 'a']) >>> s 0 b 1 a 2 a 3 b 4 a dtype: object >>> s.replace({'a': None}) 0 b 1 <NA> 2 <NA> 3 b 4 <NA> dtype: object如果
to_replace和value中的值類型與實際係列不匹配,則 cudf 對 pandas 表現出不同的行為,並且這些對將被靜默忽略:>>> s = cudf.Series(['b', 'a', 'a', 'b', 'a']) >>> s 0 b 1 a 2 a 3 b 4 a dtype: object >>> s.replace('a', 1) 0 b 1 a 2 a 3 b 4 a dtype: object >>> s.replace(['a', 'c'], [1, 2]) 0 b 1 a 2 a 3 b 4 a dtype: objectDataFrame
標量
to_replace和value>>> import cudf >>> df = cudf.DataFrame({'A': [0, 1, 2, 3, 4], ... 'B': [5, 6, 7, 8, 9], ... 'C': ['a', 'b', 'c', 'd', 'e']}) >>> df A B C 0 0 5 a 1 1 6 b 2 2 7 c 3 3 8 d 4 4 9 e >>> df.replace(0, 5) A B C 0 5 5 a 1 1 6 b 2 2 7 c 3 3 8 d 4 4 9 eList-like
to_replace>>> df.replace([0, 1, 2, 3], 4) A B C 0 4 5 a 1 4 6 b 2 4 7 c 3 4 8 d 4 4 9 e >>> df.replace([0, 1, 2, 3], [4, 3, 2, 1]) A B C 0 4 5 a 1 3 6 b 2 2 7 c 3 1 8 d 4 4 9 edict-like
to_replace>>> df.replace({0: 10, 1: 100}) A B C 0 10 5 a 1 100 6 b 2 2 7 c 3 3 8 d 4 4 9 e >>> df.replace({'A': 0, 'B': 5}, 100) A B C 0 100 100 a 1 1 6 b 2 2 7 c 3 3 8 d 4 4 9 e
相關用法
- Python cudf.DataFrame.repeat用法及代碼示例
 - Python cudf.DataFrame.resample用法及代碼示例
 - Python cudf.DataFrame.reset_index用法及代碼示例
 - Python cudf.DataFrame.rename用法及代碼示例
 - Python cudf.DataFrame.reindex用法及代碼示例
 - Python cudf.DataFrame.rmul用法及代碼示例
 - Python cudf.DataFrame.rfloordiv用法及代碼示例
 - Python cudf.DataFrame.round用法及代碼示例
 - Python cudf.DataFrame.rpow用法及代碼示例
 - Python cudf.DataFrame.radd用法及代碼示例
 - Python cudf.DataFrame.rdiv用法及代碼示例
 - Python cudf.DataFrame.rsub用法及代碼示例
 - Python cudf.DataFrame.rolling用法及代碼示例
 - Python cudf.DataFrame.rmod用法及代碼示例
 - Python cudf.DataFrame.rtruediv用法及代碼示例
 - Python cudf.DataFrame.mod用法及代碼示例
 - Python cudf.DataFrame.isin用法及代碼示例
 - Python cudf.DataFrame.apply用法及代碼示例
 - Python cudf.DataFrame.exp用法及代碼示例
 - Python cudf.DataFrame.drop用法及代碼示例
 
注:本文由純淨天空篩選整理自rapids.ai大神的英文原創作品 cudf.DataFrame.replace。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
