**pandas.DataFrame.where():Replace values where the condition is False.将不符合条件的值,替换成指定值。
pandas.DataFrame.mask():Replace values where the condition is True…将符合条件的值,替换成指定值。
**
待读取文件预览:
![](https://img-blog.csdnimg.cn/09bf4dfe49544a9980f2af7db7d948a5.png)
import pandas as pd
# 读取excel
file_path = r'C:\Users\123\Desktop\pandastest\example.xlsx'
df = pd.read_excel(file_path, sheet_name=0, header=0, skipfooter=0)
df1 = df.copy()
# 预览原dataframe
print(df)
# condition 判断价格是否等于999.0
condition = df["价格"] == 999.0
# 不符合调节的,替换成99
where_series = df["价格"].where(condition, "99")
df["价格"] = where_series
print(df)
# condition 判断价格是否等于999.0
condition1 = df1["价格"] == 999.0
# 符合条件的的,替换成99
where_series1 = df1["价格"].mask(condition1, "99")
df1["价格"] = where_series1
print(df1)
运行结果: