Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Python Pandas - Basics of MultiIndex



MultiIndex is also called Hierarchical Indexing, it is a powerful feature in pandas that allows you to work with higher-dimensional data in lower-dimensional structures like Series (1D) and DataFrame (2D). With MultiIndex, pandas objects have multiple levels of index labels. Using MultiIndex, you can represent and manipulate data with multiple levels of indexing, making it easier to handle complex data sets efficiently.

In this tutorial, we will learn about the basics of MultiIndex, including how to create MultiIndexed Series and DataFrames, perform basic indexing on MultiIndex axes, and align data using MultiIndex.

Creating MultiIndexed Pandas Objects

There are several ways to create a MultiIndex object in pandas, including from lists of arrays, tuples, products of iterables, or directly from a DataFrame.

Following are the list of helper methods to construct a new MultiIndex −

  • MultiIndex.from_arrays()

  • MultiIndex.from_product()

  • MultiIndex.from_tuples()

  • MultiIndex.from_frame()

Creating MultiIndex from Lists of Arrays

By using thepandas.MultiIndex.from_arrays() method we can create MultiIndex from list of arrays.

Example: Creating MultiIndexed Series from List of lists

The following example demonstrates the creation of MultiIndexed Series object using thepandas.MultiIndex.from_arrays() method.

import pandas as pdimport numpy as np# Create a 2D listlist_2d = [["BMW", "BMW", "Lexus", "Lexus", "foo", "foo", "Audi", "Audi"],["1", "2", "1", "2", "1", "2", "1", "2"]]# Create a MultiIndex objectindex = pd.MultiIndex.from_arrays(list_2d, names=["first", "second"])# Creating a MultiIndexed Seriess = pd.Series(np.random.randn(8), index=index)# Display the output Series print("Output MultiIndexed Series:\n",s)

Following is the output of the above code −

Output MultiIndexed Series:
FirstSecond
BMW1-1.334159
2-0.233286
Lexus11.167558
2-0.875364
foo1-0.338715
20.021517
Audi1-0.272688
20.588359
dtype: float64

Creating MultiIndex from Tuples

PandasMultiIndex.from_tuples() method is used to convert list of tuples to MultiIndex.

Example: Creating MultiIndexed DataFrame from Tuples

This example demonstrates the creation of MultiIndexed DataFrame object using thepandas.MultiIndex.from_tuples() method.

import pandas as pdimport numpy as np# Create a 2D listlist_2d = [["BMW", "BMW", "Lexus", "Lexus", "foo", "foo", "Audi", "Audi"],["1", "2", "1", "2", "1", "2", "1", "2"]]# Create a MultiIndex objecttuples = list(zip(*list_2d ))index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])# Creating a MultiIndexed DataFramedf = pd.DataFrame(np.random.randn(8, 4), index=index, columns=["A", "B", "C", "D"])# Display the output Series print("Output MultiIndexed DataFrame:\n", df)

Following is the output of the above code −

Output MultiIndexed DataFrame:
ABCD
FirstSecond
BMW1-0.3478461.011760-1.224244-1.225259
20.237115-1.3164330.962960-1.008623
Lexus1-1.8062091.0389730.246994-1.596616
2-0.325284-0.264822-0.7350290.377645
foo10.243560-0.4087180.717466-1.446259
2-0.8177040.7112992.567860-1.054871
Audi10.583519-0.0075770.828928-0.826645
22.4546261.5580450.981507-0.148554

Creating MultiIndex Using from_product()

The PandasMultiIndex.from_product() method is uses the cartesian product of multiple iterables to create MultiIndex. It is useful when you want every possible combination of elements from two or more iterables.

Example: Creating MultiIndexed DataFrame Using from_product()

This example demonstrates how to create the MultiIndexed DataFrame using the pandasMultiIndex.from_product() method.

import pandas as pdimport numpy as np# Create a list of lits iterable = [[1, 2, 3], ['green', 'black']]# Create a MultiIndex objectindex = pd.MultiIndex.from_product(iterable, names=["number", "color"])# Creating a MultiIndexed DataFramedf = pd.DataFrame(np.random.randn(6, 3), index=index, columns=["A", "B", "C"])# Display the output Series print("Output MultiIndexed DataFrame:\n", df)

Following is the output of the above code −

Output MultiIndexed DataFrame:
ABC
NumberColor
1green-1.174910-0.861695-0.026601
black-2.8242890.6748701.132675
2green-0.285381-0.1041881.993371
black-0.926109-0.579404-1.119692
3green-3.278989-0.873407-1.359360
black0.7354920.066735-0.099568

Creating MultiIndex from DataFrame

The PandasMultiIndex.from_frame() method is used to create a MultiIndex from a DataFrame.

Example: Creating MultiIndex from DataFrame

This example uses thepd.MultiIndex.from_frame() method to directly create a MultiIndex object from a DataFrame.

import pandas as pdimport numpy as np# Create a DataFramedf = pd.DataFrame([["BMW", 1], ["BMW", 2], ["Lexus", 1],["Lexus", 2]],  columns=["first", "second"])# Create a MultiIndex objectindex = pd.MultiIndex.from_frame(df)# Creating a MultiIndexed DataFramedf = pd.DataFrame(np.random.randn(4, 3), index=index, columns=["A", "B", "C"])# Display the output Series print("Output MultiIndexed DataFrame:\n", df)

Following is the output of the above code −

Output MultiIndexed DataFrame:
ABC
FirstSecond
BMW1-0.662779-0.2707750.129462
2-0.2513081.9208960.756204
Lexus1-0.4661330.5908720.252439
2-1.226775-0.239043-0.023214

Basic Indexing on Axis with MultiIndex

Indexing with MultiIndex used to slice and select data in more flexible ways compared to a regular index.

Example: Selecting Data by Index Level

Here is a basic example demonstrating the indexing MultiIndexed Series object using the.loc[] method.

import pandas as pdimport numpy as np# Creating MultiIndex from arraysarrays = [["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],["one", "two", "one", "two", "one", "two", "one", "two"]]# Creating a list of tuples from the arraystuples = list(zip(*arrays))# Creating a MultiIndex from tuplesindex = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])# Creating a Series with MultiIndexs = pd.Series([2, 3, 1, 4, 6, 1, 7, 8], index=index)print("MultiIndexed Series:\n", s)# Indexing the MultiIndexed Series using .loc[]print("\nSelecting data at index ('bar', 'one') and column 'A':")print(s.loc[('bar', 'one')])

Following is the output of the above code −

MultiIndexed Series:
FirstSecond
barone2
two2
one11
two4
one16
two1
one17
two8
dtype: int64Selecting data at index ('bar', 'one') and column 'A':2
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp