大江哥 发表于 2023-2-23 16:44:19

pandas条件替换值(where&mask)

pandas条件替换值(where&mask)

在日常分析中,经常会遇到对数据的筛选处理相关的工作,我们可以使用loc和iloc定位分析筛选的列或行数据,下面介绍一种高级筛选的用法where和mask。
pd.where: 替换条件(condition)为Flase处的值
pd.mask: 替换条件(condition)为True处的值
np.where: 替换条件,类似三元表达式
# 条件不成立时,值替换成other
pd.where(self, cond, other=nan, inplace=False,
      axis=None, level=None, errors='raise', try_cast=False)
# 条件成立时,值替换成other
pd.mask(self, cond, other=nan, inplace=False,
                axis=None, level=None, errors='raise', try_cast=False)
# 条件成立时,值为x;不成立时,值为y
np.where(condition, x, y)首先模拟一组学生成绩表数据:
import pandas as pd
import numpy as np

# 设置学科
subjects = ['math', 'chinese', 'english', 'history']

# 设置学生
students = ['Tom', 'Alice', 'Bobby', 'Candy', 'David', 'Eva', 'Frank', 'Grace', 'Howard', 'Ivy',
            'John', 'Karen', 'Larry', 'Marie', 'Nancy', 'Oscar', 'Peter', 'Queen', 'Robert', 'Susan']

# 随机生成成绩
score = np.random.randint(low=0, high=100, size=(len(students), len(subjects)))

# 生成DataFrame
df = pd.DataFrame(
    score,
    columns=subjects,
    index=students
)dfmathchineseenglishhistoryTom24576044Alice92256426Bobby96619496Candy36871038David29733764Eva94403081Frank24444014Grace3770505Howard82864610Ivy2473030John39329748Karen68293411Larry825378Marie96837363Nancy25333753Oscar2654973Peter9191167Queen44198523Robert75354777Susan71610821 pd.where


[*]where(条件, pd.NA)
值替换:pandas中的where方法,如果 条件 为真,保持原来的值,否则替换为other
增加字段 math_pass, 数学成绩大于60,为及格, 否则为不及格
df1 = df.copy()
# 默认及格
df1['math_pass'] = '及格'
df1['math_pass'] = df1['math_pass'].where(df1['math'] > 60, '不及格')df1mathchineseenglishhistorymath_passTom24576044不及格Alice92256426及格Bobby96619496及格Candy36871038不及格David29733764不及格Eva94403081及格Frank24444014不及格Grace3770505不及格Howard82864610及格Ivy2473030不及格John39329748不及格Karen68293411及格Larry825378及格Marie96837363及格Nancy25333753不及格Oscar2654973不及格Peter9191167不及格Queen44198523不及格Robert75354777及格Susan7161082及格2 np.where

在numpy中的where使用,与pandas有所不同
# 条件成立时,值为x;不成立时,值为y
np.where(condition, x, y)增加字段 math_pass2, 数学成绩大于60,为及格, 否则为不及格
df2 = df.copy()
# 数学成绩大于60,为及格, 否则为不及格
df2['math_pass2'] = np.where(df2['math'] > 60, '及格', '不及格')df2mathchineseenglishhistorymath_pass2Tom24576044不及格Alice92256426及格Bobby96619496及格Candy36871038不及格David29733764不及格Eva94403081及格Frank24444014不及格Grace3770505不及格Howard82864610及格Ivy2473030不及格John39329748不及格Karen68293411及格Larry825378及格Marie96837363及格Nancy25333753不及格Oscar2654973不及格Peter9191167不及格Queen44198523不及格Robert75354777及格Susan7161082及格3 pd.mask

值替换:pandas中的mask方法,如果 条件 为真,值替换为other
增加字段 math_pass3, 数学成绩大于60,为及格, 否则为不及格
df3 = df.copy()
df3['math_pass3'] = '不及格'
df3['math_pass3'] = df3['math_pass3'].mask(df3['math'] > 60, '及格')df3mathchineseenglishhistorymath_pass3Tom24576044不及格Alice92256426及格Bobby96619496及格Candy36871038不及格David29733764不及格Eva94403081及格Frank24444014不及格Grace3770505不及格Howard82864610及格Ivy2473030不及格John39329748不及格Karen68293411及格Larry825378及格Marie96837363及格Nancy25333753不及格Oscar2654973不及格Peter9191167不及格Queen44198523不及格Robert75354777及格Susan7161082及格
来源:https://www.cnblogs.com/itelephant/p/17147902.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: pandas条件替换值(where&mask)