Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.MultiIndex.from_frame#

classmethodMultiIndex.from_frame(df,sortorder=None,names=None)[source]#

Make a MultiIndex from a DataFrame.

Parameters:
dfDataFrame

DataFrame to be converted to MultiIndex.

sortorderint, optional

Level of sortedness (must be lexicographically sorted by thatlevel).

nameslist-like, optional

If no names are provided, use the column names, or tuple of columnnames if the columns is a MultiIndex. If a sequence, overwritenames with the given sequence.

Returns:
MultiIndex

The MultiIndex representation of the given DataFrame.

See also

MultiIndex.from_arrays

Convert list of arrays to MultiIndex.

MultiIndex.from_tuples

Convert list of tuples to MultiIndex.

MultiIndex.from_product

Make a MultiIndex from cartesian product of iterables.

Examples

>>>df=pd.DataFrame([['HI','Temp'],['HI','Precip'],...['NJ','Temp'],['NJ','Precip']],...columns=['a','b'])>>>df      a       b0    HI    Temp1    HI  Precip2    NJ    Temp3    NJ  Precip
>>>pd.MultiIndex.from_frame(df)MultiIndex([('HI',   'Temp'),            ('HI', 'Precip'),            ('NJ',   'Temp'),            ('NJ', 'Precip')],           names=['a', 'b'])

Using explicit names, instead of the column names

>>>pd.MultiIndex.from_frame(df,names=['state','observation'])MultiIndex([('HI',   'Temp'),            ('HI', 'Precip'),            ('NJ',   'Temp'),            ('NJ', 'Precip')],           names=['state', 'observation'])

[8]ページ先頭

©2009-2025 Movatter.jp