pandas-dev / pandas Public
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Drop Index Combinations from Multiindex Dataframe #43793
Comments
Thanks for the report @juliandwain ! In [2]: import pandas as pd
In [3]: midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
...: ... ['speed', 'weight', 'length']],
...: ... codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
...: ... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
...: >>> df = pd.DataFrame(index=midx, columns=['big', 'small'],
...: ... data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
...: ... [250, 150], [1.5, 0.8], [320, 250],
...: ... [1, 0.8], [0.3, 0.2]])
In [4]: df
Out[4]:
big small
lama speed 45.0 30.0
weight 200.0 100.0
length 1.5 1.0
cow speed 30.0 20.0
weight 250.0 150.0
length 1.5 0.8
falcon speed 320.0 250.0
weight 1.0 0.8
length 0.3 0.2
In [5]: df.index
Out[5]:
MultiIndex([( 'lama', 'speed'),
( 'lama', 'weight'),
( 'lama', 'length'),
( 'cow', 'speed'),
( 'cow', 'weight'),
( 'cow', 'length'),
('falcon', 'speed'),
('falcon', 'weight'),
('falcon', 'length')],
)
In [6]: df.drop(labels=('cow','speed'))
Out[6]:
big small
lama speed 45.0 30.0
weight 200.0 100.0
length 1.5 1.0
cow weight 250.0 150.0
length 1.5 0.8
falcon speed 320.0 250.0
weight 1.0 0.8
length 0.3 0.2 |
Oh, it was as easy at it is. |
Right! |
Yes I can do that! |
If you need any help, I'll be happy to help. I'd like to start contributing. |
take |
Is your feature request related to a problem?
I whish the pandas drop function would let me drop combination of rows (or columns) in multiindex dataframes.
E.g., I have a multiindex dataframe like this
And I want to drop only the Index combination
(B, a)
.With the current implementation of the drop function, this is not possible.
Describe the solution you'd like
Maybe adding an additional parameter called
combination
, which is a boolean value, could solve the problem.When setting
combination=True
, the list-likelabel
parameter could be recognized as the combination to drop, which would then result inAPI breaking implications
I think adding the parameter would not break the API.
Describe alternatives you've considered
I have not considered alternatives
Additional context
For my problem, I implemented a function similar to the one below
The text was updated successfully, but these errors were encountered: