pandas條件替換值(where&mask)

pandas條件替換值(where&mask)

在日常分析中,經常會遇到對數據的篩選處理相關的工作,我們可以使用lociloc定位分析篩選的列或行數據,下面介紹一種高級篩選的用法wheremask

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
)
df
math chinese english history
Tom 24 57 60 44
Alice 92 25 64 26
Bobby 96 61 94 96
Candy 36 87 10 38
David 29 73 37 64
Eva 94 40 30 81
Frank 24 44 40 14
Grace 37 70 50 5
Howard 82 86 46 10
Ivy 24 7 30 30
John 39 32 97 48
Karen 68 29 34 11
Larry 82 5 3 78
Marie 96 83 73 63
Nancy 25 33 37 53
Oscar 2 65 49 73
Peter 9 19 11 67
Queen 44 19 85 23
Robert 75 35 47 77
Susan 71 6 10 82

1 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, '不及格')
df1
math chinese english history math_pass
Tom 24 57 60 44 不及格
Alice 92 25 64 26 及格
Bobby 96 61 94 96 及格
Candy 36 87 10 38 不及格
David 29 73 37 64 不及格
Eva 94 40 30 81 及格
Frank 24 44 40 14 不及格
Grace 37 70 50 5 不及格
Howard 82 86 46 10 及格
Ivy 24 7 30 30 不及格
John 39 32 97 48 不及格
Karen 68 29 34 11 及格
Larry 82 5 3 78 及格
Marie 96 83 73 63 及格
Nancy 25 33 37 53 不及格
Oscar 2 65 49 73 不及格
Peter 9 19 11 67 不及格
Queen 44 19 85 23 不及格
Robert 75 35 47 77 及格
Susan 71 6 10 82 及格

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, '及格', '不及格')
df2
math chinese english history math_pass2
Tom 24 57 60 44 不及格
Alice 92 25 64 26 及格
Bobby 96 61 94 96 及格
Candy 36 87 10 38 不及格
David 29 73 37 64 不及格
Eva 94 40 30 81 及格
Frank 24 44 40 14 不及格
Grace 37 70 50 5 不及格
Howard 82 86 46 10 及格
Ivy 24 7 30 30 不及格
John 39 32 97 48 不及格
Karen 68 29 34 11 及格
Larry 82 5 3 78 及格
Marie 96 83 73 63 及格
Nancy 25 33 37 53 不及格
Oscar 2 65 49 73 不及格
Peter 9 19 11 67 不及格
Queen 44 19 85 23 不及格
Robert 75 35 47 77 及格
Susan 71 6 10 82 及格

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, '及格')
df3
math chinese english history math_pass3
Tom 24 57 60 44 不及格
Alice 92 25 64 26 及格
Bobby 96 61 94 96 及格
Candy 36 87 10 38 不及格
David 29 73 37 64 不及格
Eva 94 40 30 81 及格
Frank 24 44 40 14 不及格
Grace 37 70 50 5 不及格
Howard 82 86 46 10 及格
Ivy 24 7 30 30 不及格
John 39 32 97 48 不及格
Karen 68 29 34 11 及格
Larry 82 5 3 78 及格
Marie 96 83 73 63 及格
Nancy 25 33 37 53 不及格
Oscar 2 65 49 73 不及格
Peter 9 19 11 67 不及格
Queen 44 19 85 23 不及格
Robert 75 35 47 77 及格
Susan 71 6 10 82 及格
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章