9951 explained code solutions for 126 technologies


python-pandasHow to use multiple conditions in mask()


import pandas as pd

df = pd.DataFrame({
  'Id':    [1, 10, 12, 15, 34],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Phone Price': [204, 304, 404, 405, 305]
})

df = df.mask((df['Phone Price'] > 400) | (df['Phone Price'] < 300), None)ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

.mask(

masks rows by given filter with specified value

df['Phone Price'] > 400

first condition to check

df['Phone Price'] < 300

another condition to check

|

logical or (use & for logical and)

None

value to replace found values by


Usage example

import pandas as pd

df = pd.DataFrame({
  'Id':    [1, 10, 12, 15, 34],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Phone Price': [204, 304, 404, 405, 305]
})

df = df.mask((df['Phone Price'] > 400) | (df['Phone Price'] < 300), None)

print(df)
output
     Id Phone  Phone Price
0   NaN  None          NaN
1  10.0   ip6        304.0
2   NaN  None          NaN
3   NaN  None          NaN
4  34.0    xi        305.0