翼度科技»论坛 编程开发 python 查看内容

pandas条件替换值(where&mask)

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
pandas条件替换值(where&mask)

在日常分析中,经常会遇到对数据的筛选处理相关的工作,我们可以使用loc和iloc定位分析筛选的列或行数据,下面介绍一种高级筛选的用法where和mask。
pd.where: 替换条件(condition)为Flase处的值
pd.mask: 替换条件(condition)为True处的值
np.where: 替换条件,类似三元表达式
  1. # 条件不成立时,值替换成other
  2. pd.where(self, cond, other=nan, inplace=False,
  3.         axis=None, level=None, errors='raise', try_cast=False)
  4. # 条件成立时,值替换成other
  5. pd.mask(self, cond, other=nan, inplace=False,
  6.                 axis=None, level=None, errors='raise', try_cast=False)
  7. # 条件成立时,值为x;不成立时,值为y
  8. np.where(condition, x, y)
复制代码
首先模拟一组学生成绩表数据:
  1. import pandas as pd
  2. import numpy as np
  3. # 设置学科
  4. subjects = ['math', 'chinese', 'english', 'history']
  5. # 设置学生
  6. students = ['Tom', 'Alice', 'Bobby', 'Candy', 'David', 'Eva', 'Frank', 'Grace', 'Howard', 'Ivy',
  7.             'John', 'Karen', 'Larry', 'Marie', 'Nancy', 'Oscar', 'Peter', 'Queen', 'Robert', 'Susan']
  8. # 随机生成成绩
  9. score = np.random.randint(low=0, high=100, size=(len(students), len(subjects)))
  10. # 生成DataFrame
  11. df = pd.DataFrame(
  12.     score,
  13.     columns=subjects,
  14.     index=students
  15. )
复制代码
  1. df
复制代码
mathchineseenglishhistoryTom24576044Alice92256426Bobby96619496Candy36871038David29733764Eva94403081Frank24444014Grace3770505Howard82864610Ivy2473030John39329748Karen68293411Larry825378Marie96837363Nancy25333753Oscar2654973Peter9191167Queen44198523Robert75354777Susan71610821 pd.where


  • where(条件, pd.NA)
值替换:pandas中的where方法,如果 条件 为真,保持原来的值,否则替换为other
增加字段 math_pass, 数学成绩大于60,为及格, 否则为不及格
  1. df1 = df.copy()
  2. # 默认及格
  3. df1['math_pass'] = '及格'
  4. df1['math_pass'] = df1['math_pass'].where(df1['math'] > 60, '不及格')
复制代码
  1. df1
复制代码
mathchineseenglishhistorymath_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有所不同
  1. # 条件成立时,值为x;不成立时,值为y
  2. np.where(condition, x, y)
复制代码
增加字段 math_pass2, 数学成绩大于60,为及格, 否则为不及格
  1. df2 = df.copy()
  2. # 数学成绩大于60,为及格, 否则为不及格
  3. df2['math_pass2'] = np.where(df2['math'] > 60, '及格', '不及格')
复制代码
  1. df2
复制代码
mathchineseenglishhistorymath_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,为及格, 否则为不及格
  1. df3 = df.copy()
  2. df3['math_pass3'] = '不及格'
  3. df3['math_pass3'] = df3['math_pass3'].mask(df3['math'] > 60, '及格')
复制代码
  1. df3
复制代码
mathchineseenglishhistorymath_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】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具