Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove rows from MultiIndex Dataframe

I have pandas multi-index dataframe that like this. I need to remove rows where bnds equals to 1.0.

I’ve tried to do df_f.drop('1.0', level=bnds, axis=0, inplace=True) as per documentation, but getting error NameError: name ‘bnds’ is not defined

                                 Honolulu        time_bnds        Seattle
bnds    time            
1.0    2015-01-01 12:00:00      70.277412   2015-01-01 00:00:00   13.346752
       2015-01-02 12:00:00      69.948593   2015-01-02 00:00:00   31.655853
       2015-01-03 12:00:00      70.228027   2015-01-03 00:00:00   31.511438
2.0    2015-01-01 12:00:00      70.277412   2015-01-01 00:00:00   13.346752
       2015-01-02 12:00:00      69.948593   2015-01-02 00:00:00   31.655853

Update: I’ve used quotes "bnds" as suggested, but got another error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

5433 def drop(
   5434     self,
   5435     labels: IndexLabel | None = None,
   (...)
   5442     errors: IgnoreRaise = "raise",
   5443 ) -> DataFrame | None:
   5444     """
   5445     Drop specified labels from rows or columns.
   5446 
   (...)
   5579             weight  1.0     0.8
   5580     """
-> 5581     return super().drop(
   5582         labels=labels,
   5583         axis=axis,
   5584         index=index,
   5585         columns=columns,
   5586         level=level,
   5587         inplace=inplace,
   5588         errors=errors,
   5589     )

File /srv/conda/envs/notebook/lib/python3.11/site-packages/pandas/core/generic.py:4788, in NDFrame.drop(self, labels, axis, index, columns, level, inplace, errors)
   4786 for axis, labels in axes.items():
   4787     if labels is not None:
-> 4788         obj = obj._drop_axis(labels, axis, level=level, errors=errors)
   4790 if inplace:
   4791     self._update_inplace(obj)

File /srv/conda/envs/notebook/lib/python3.11/site-packages/pandas/core/generic.py:4828, in NDFrame._drop_axis(self, labels, axis, level, errors, only_slice)
   4826     if not isinstance(axis, MultiIndex):
   4827         raise AssertionError("axis must be a MultiIndex")
-> 4828     new_axis = axis.drop(labels, level=level, errors=errors)
   4829 else:
   4830     new_axis = axis.drop(labels, errors=errors)

File /srv/conda/envs/notebook/lib/python3.11/site-packages/pandas/core/indexes/multi.py:2408, in MultiIndex.drop(self, codes, level, errors)
   2361 """
   2362 Make a new :class:`pandas.MultiIndex` with the passed list of codes deleted.
   2363 
   (...)
   2405            names=['number', 'color'])
   2406 """
   2407 if level is not None:
-> 2408     return self._drop_from_level(codes, level, errors)
   2410 if not isinstance(codes, (np.ndarray, Index)):
   2411     try:

File /srv/conda/envs/notebook/lib/python3.11/site-packages/pandas/core/indexes/multi.py:2462, in MultiIndex._drop_from_level(self, codes, level, errors)
   2460 not_found = codes[values == -2]
   2461 if len(not_found) != 0 and errors != "ignore":
-> 2462     raise KeyError(f"labels {not_found} not found in level")
   2463 mask = ~algos.isin(self.codes[i], values)
   2465 return self[mask]

KeyError: "labels ['1.0'] not found in level"
    enter code here

>Solution :

Your issue is that you are passing '1.0' as string while your bnds column is obviously not of string type. Try running the following:

df_f.drop(1.0, level="bnds", axis=0, inplace=True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading