DataFrame.where(selfcondother=naninplace=Falseaxis=Nonelevel=Noneerrors=’raise’try_cast=False)

  • cond :条件,当条件满足时保留原始值,不满足,用other代替
  • other: cond不满足时,替换的值,默认是nan
  • inplace: 决定是否对原始数据进行操作
  • return 输入的格式

用于条件筛选,符合条件留下,否则进行相应操作。

DataFrame.mask(selfcondother=naninplace=Falseaxis=Nonelevel=Noneerrors=’raise’try_cast=False)

参数与where一致,条件刚好相反

#example of Series


>>> s = pd.Series(range(5))
>>> s
0    0
1    1
2    2
3    3
4    4
dtype: int64

>>> s.where(s > 0)
0    NaN
1    1.0
2    2.0
3    3.0
4    4.0
dtype: float64

>>> s.where(s > 1, 10)
0    10
1    10
2    2
3    3
4    4
dtype: int64

>>> s.mask(s > 0)
0    0.0
1    NaN
2    NaN
3    NaN
4    NaN
dtype: float64

#example of DataFrame


>>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
>>> df
   A  B
0  0  1
1  2  3
2  4  5
3  6  7
4  8  9

>>> df.where(df % 3 == 0, -df)
   A  B
0  0 -1
1 -2  3
2 -4 -5
3  6 -7
4 -8  9

>>> df_orig = df.copy()
>>> df_orig.where(df > 2, -df, inplace=True)
>>> df_orig
   A  B
0  0 -1
1 -2  3
2  4  5
3  6  7
4  8  9

>>> df.where(lambda x: x > 4, lambda x: x + 10)
    A   B
0  10  11
1  12  13
2  14   5
3   6   7
4   8   9
Categories: pandasPython

0 Comments

Leave a Reply

Your email address will not be published.