9951 explained code solutions for 126 technologies


python-pandasHow to use 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.mask(df['Phone Price'] > 400, 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

filter to mask rows by

None

value to replace found values to


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, None)

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