'Problem while trying to delete row with certain value

I have a problem while trying to delete row: Error:

ValueError                                Traceback (most recent call last)
<ipython-input-186-83339e440bcb> in <module>()
      1 df.head()
      2 df['bathrooms'] = df['bathrooms'].astype('int64')
----> 3 df['bathrooms'] = df[df['bathrooms'] != 28]

1 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py in _set_item_frame_value(self, key, value)
   3727             len_cols = 1 if is_scalar(cols) else len(cols)
   3728             if len_cols != len(value.columns):
-> 3729                 raise ValueError("Columns must be same length as key")
   3730 
   3731             # align right-hand-side columns if self.columns

ValueError: Columns must be same length as key

Code:

df['bathrooms'] = df['bathrooms'].astype('int64')
df['bathrooms'] = df[df['bathrooms'] != 28]

dataframe: enter image description here

Any help is appreciated very



Solution 1:[1]

df['bathrooms'] != 28 gives you bool values.

df[df['bathrooms'] != 28] gives you a dataframe. then you are assigning a dataframe to a column. df['bathrooms'] = df[df['bathrooms'] != 28]

If you want a new dataframe you can do:

df = df[df['bathrooms'] != 28]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 scapula13