Python multiindex drop level

pandas.MultiIndex.drop#

Make a new pandas.MultiIndex with the passed list of codes deleted.

Parameters : codes array-like

Must be a list of tuples when level is not specified.

level int or level name, default None errors str, default ‘raise’ Returns : MultiIndex

>>> idx = pd.MultiIndex.from_product([(0, 1, 2), ('green', 'purple')], . names=["number", "color"]) >>> idx MultiIndex([(0, 'green'), (0, 'purple'), (1, 'green'), (1, 'purple'), (2, 'green'), (2, 'purple')], names=['number', 'color']) >>> idx.drop([(1, 'green'), (2, 'purple')]) MultiIndex([(0, 'green'), (0, 'purple'), (1, 'purple'), (2, 'green')], names=['number', 'color']) 

We can also drop from a specific level.

>>> idx.drop('green', level='color') MultiIndex([(0, 'purple'), (1, 'purple'), (2, 'purple')], names=['number', 'color']) 
>>> idx.drop([1, 2], level=0) MultiIndex([(0, 'green'), (0, 'purple')], names=['number', 'color']) 

Источник

pandas.MultiIndex.droplevel#

If resulting index has only 1 level left, the result will be of Index type, not MultiIndex. The original index is not modified inplace.

Parameters level int, str, or list-like, default 0

If a string is given, must be the name of a level If list-like, elements must be names or indexes of levels.

Returns Index or MultiIndex

>>> mi = pd.MultiIndex.from_arrays( . [[1, 2], [3, 4], [5, 6]], names=['x', 'y', 'z']) >>> mi MultiIndex([(1, 3, 5), (2, 4, 6)], names=['x', 'y', 'z']) 
>>> mi.droplevel() MultiIndex([(3, 5), (4, 6)], names=['y', 'z']) 
>>> mi.droplevel(2) MultiIndex([(1, 3), (2, 4)], names=['x', 'y']) 
>>> mi.droplevel('z') MultiIndex([(1, 3), (2, 4)], names=['x', 'y']) 
>>> mi.droplevel(['x', 'y']) Index([5, 6], dtype='int64', name='z') 

Источник

pandas.MultiIndex.drop#

Make a new pandas.MultiIndex with the passed list of codes deleted.

Parameters : codes array-like

Must be a list of tuples when level is not specified.

level int or level name, default None errors str, default ‘raise’ Returns : MultiIndex

>>> idx = pd.MultiIndex.from_product([(0, 1, 2), ('green', 'purple')], . names=["number", "color"]) >>> idx MultiIndex([(0, 'green'), (0, 'purple'), (1, 'green'), (1, 'purple'), (2, 'green'), (2, 'purple')], names=['number', 'color']) >>> idx.drop([(1, 'green'), (2, 'purple')]) MultiIndex([(0, 'green'), (0, 'purple'), (1, 'purple'), (2, 'green')], names=['number', 'color']) 

We can also drop from a specific level.

>>> idx.drop('green', level='color') MultiIndex([(0, 'purple'), (1, 'purple'), (2, 'purple')], names=['number', 'color']) 
>>> idx.drop([1, 2], level=0) MultiIndex([(0, 'green'), (0, 'purple')], names=['number', 'color']) 

Источник

How to Drop a Level from a MultiIndex in Pandas DataFrame

Here are several approaches to drop levels of MultiIndex in a Pandas DataFrame:

  • droplevel — completely drop MultiIndex level
  • reset_index — remove levels of MultiIndex while storing data into columns/rows

Step 1: Pandas drop MultiIndex by method — droplevel

Pandas drop MultiIndex on index/rows

Method droplevel() will remove one, several or all levels from a MultiIndex. Let’s check the default execution by next example:

import pandas as pd cols = pd.MultiIndex.from_tuples([(0, 1), (0, 1)]) df = pd.DataFrame([[1,2], [3,4]], index=cols) df 

We can drop level 0 of the MultiIndex by:

Pandas drop MultiIndex on columns

If the hierarchical indexing is on the columns then we can drop levels by parameter axis :

Get column names of the dropped columns

If you like to get the names of the columns which will be dropped you can use next syntax:

which will result of single index:

Pandas drop MultiIndex with .columns.droplevel(level=0)

So we can drop level of MultiIndex with a simple reset of the column names like:

df.columns = df.columns.droplevel(level=0) 

drop-level-from-multiindex-pandas

Step 2: Pandas drop MultiIndex to column values by reset_index

Drop all levels of MultiIndex to columns

Use reset_index if you like to drop the MultiIndex while keeping the information from it. Let’s do a quick demo:

import pandas as pd cols = pd.MultiIndex.from_tuples([(0, 1), (0, 1)]) df = pd.DataFrame([[1,2], [3,4]], index=cols) 

After reset of the MultiIndex we will get:

Reset single level of MultiIndex

Typical Errors on Pandas Drop MultiIndex

When the droplevel is invoked on wrong axis: columns or rows like:

cols = pd.MultiIndex.from_tuples([(0, 1), (0, 1)]) df = pd.DataFrame([[1,2], [3,4]], index=cols) df.columns.droplevel() 

ValueError: Cannot remove 1 levels from an index with 1 levels: at least one level must be left.

Another example is when the levels are less the one which should be dropped:

cols = pd.MultiIndex.from_tuples([(0, 1), (0, 1)]) df = pd.DataFrame([[1,2], [3,4]], index=cols) df.columns.droplevel(level=3) 

IndexError: Too many levels: Index has only 1 level, not 4

Resources

Источник

Читайте также:  Название документа
Оцените статью