pandas dataframe dataframe.replace()函数
pandas.dataframe.replace() 用其他值替换 dataframe 中的值,这些值可以是字符串、正则表达式、列表、字典、series
或数字。
pandas.dataframe.replace()
语法
dataframe.replace(,
to_replace=none,
value=none,
inplace=false,
limit=none,
regex=false,
method='pad')
参数
to_replace |
字符串,正则表达式,列表,字典,系列,数字或 none . 需要替换的 dataframe 中的值。 |
value |
标量,字典,列表,字符串,正则表达式或 none . 替换任何与 to_replace 匹配的值。 |
inplace |
布尔值。如果为 true 修改调用者的 dataframe |
limit |
整数。向前或向后填充的最大间隙大小 |
regex |
布尔型。如果 to_replace 和/或 value 是一个正则表达式,则将 regex 设为 true 。 |
method |
用于替换的方法 |
返回值
它返回一个 dataframe
,用给定的 value
替换所有指定的字段。
示例代码:使用 pandas.dataframe.replace()
替换 dataframe 中的值
import pandas as pd
df = pd.dataframe({'x': [1, 2, 3,],
'y': [4, 1, 8]})
print("before replacement")
print(df)
replaced_df=df.replace(1, 5)
print("after replacement")
print(replaced_df)
输出:
before replacement
x y
0 1 4
1 2 1
2 3 8
after replacement
x y
0 5 4
1 2 5
2 3 8
这里,1
代表 to_replace
参数,5
代表 replace()
方法中的 value
参数。因此,在 df
中,所有值为 1
的元素都被 5
取代。
示例代码:在 dataframe 中使用 pandas.dataframe.replace()
替换多个值
使用列表替换
import pandas as pd
df = pd.dataframe({'x': [1, 2, 3,],
'y': [4, 1, 8]})
print("before replacement")
print(df)
replaced_df=df.replace([1,2,3],[1,4,9])
print("after replacement")
print(replaced_df)
输出:
before replacement
x y
0 1 4
1 2 1
2 3 8
after replacement
x y
0 1 4
1 4 1
2 9 8
这里,[1,2,3]
代表 to_replace
参数,[1,4,9]
代表 replace()
方法中的 value
参数。因此,在 df
中,列 [1,2,3]
被 [1,4,9]
替换。
使用字典替换
import pandas as pd
df = pd.dataframe({'x': [1, 2, 3,],
'y': [3, 1, 8]})
print("before replacement")
print(df)
replaced_df=df.replace({1:10,3:30})
print("after replacement")
print(replaced_df)
输出:
before replacement
x y
0 1 3
1 2 1
2 3 8
after replacement
x y
0 10 30
1 2 10
2 30 8
它将所有值为 1
的元素替换为 10
,将所有值为 3
的元素替换为 30
。
使用 regex
替换
import pandas as pd
df = pd.dataframe({'x': ["zeppy", "amid", "amily"],
'y': ["xar", "abc", "among"]})
print("before replacement")
print(df)
df.replace(to_replace=r'^ami.$', value='song', regex=true,inplace=true)
print("after replacement")
print(df)
输出:
before replacement
x y
0 zeppy xar
1 amid abc
2 amily among
after replacement
x y
0 zeppy xar
1 song abc
2 amily among
它将所有前三个字符为 ami
然后后面跟任何一个字符的元素替换为 song
。这里只有 amid
满足给定的正则表达式,因此只有 amid
被 song
替换。虽然 amily
的前三个字符是 ami
,但 ami
后面还有两个字符。所以,amily
不满足给定的 regex,因此它保持一样,没被替换。如果你使用的是正则表达式,请确保 regex
设置为 true
,并且 inplace=true
在调用 replace()
方法后修改原来的 dataframe
。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串