Python|熊猫 MultiIndex.levshape

原文:https://www . geesforgeks . org/python-pandas-multi index-lev shape/

Python 是进行数据分析的优秀语言,主要是因为以数据为中心的 python 包的奇妙生态系统。 【熊猫】 就是其中一个包,让导入和分析数据变得容易多了。

熊猫 **MultiIndex.levshape**属性输出一个包含多索引中每个级别长度的元组。

语法: MultiIndex.levshape

示例#1: 使用MultiIndex.levshape属性查找多索引中每个级别的长度。

# importing pandas as pd
import pandas as pd

# Creating the array
array =[[1, 2, 3], ['Sharon', 'Nick', 'Bailey']]

# Print the array
print(array)

输出:

现在让我们使用这个数组创建多索引

# Creating the MultiIndex
midx = pd.MultiIndex.from_arrays(array, names =('Number', 'Names'))

# Print the MultiIndex
print(midx)

输出:

现在我们将在多索引中找到每个级别的长度。

# Print the length of each level in MultiIndex
midx.levshape

输出: 正如我们在输出中看到的,midx MultiIndex 中每个级别的长度是(3,3)。

示例 2: 使用MultiIndex.levshape属性查找给定多索引中每个级别的长度。

# importing pandas as pd
import pandas as pd

# Creating the array
array = [[1, 2, 3], ['Sharon', 'Nick', 'Bailey'],
                   ['Doctor', 'Scientist', 'Physicist']]

# Print the array
print(array)

输出:

现在让我们使用这个数组创建多索引

# Creating the MultiIndex
midx = pd.MultiIndex.from_arrays(array, names = ('Ranking', 'Names', 'Profession'))

# Print the MultiIndex
print(midx)

输出:

现在我们将在多索引中找到每个级别的长度。

# Print the length of each levels in MultiIndex
midx.levshape

输出: 在输出中我们可以看到, midx 中每一级的长度都是 3。