どのように私はPythonのデータフレームのインデックスを組み合わせることができますか?

jaffry_x:

以下では、私は、共通のインデックスを持つすべてのエントリのための単一のインデックスを作ることができます。

cric = pd.Series(['India', 'Pakistan', 'South Africa', 'England', 'New Zealand'], 
                 index = ['Cricket', 'Cricket', 'Cricket', 'Cricket', 'Cricket'])
ftbl = pd.Series(['England', 'South Africa', 'Australia', 'Netherlands', 'New Zealand'], 
                 index = ['Football', 'Football', 'Football', 'Football' , 'Football'])
hock = pd.Series(['India', 'Pakistan', 'South Korea', 'England', 'India', 'New Zealand'], 
                 index = ['Hockey', 'Hockey', 'Hockey', 'Hockey', 'Hockey', 'Hockey'])

all_countries_1 = cric.append(ftbl)
all_countries_1 = all_countries_1.append(ftbl)
all_countries_1 = all_countries_1.append(hock)
all_countries_1 = all_countries_1.to_frame()
all_countries_1.columns = ['Countries']
all_countries_1

実際のデータ

私はアウトとして以下のことをしたいです 予想される出力

クリス:

これは、あなたが探しているものですか?

# zip the first three chars of the index and the index together
z = list(zip(all_countries_1.index.str[:3], all_countries_1.index))
# create multi index
idx = pd.MultiIndex.from_tuples(z)
# assign index
all_countries_1.index = idx

                 Countries
Cri Cricket          India
    Cricket       Pakistan
    Cricket   South Africa
    Cricket        England
    Cricket    New Zealand
Foo Football       England
    Football  South Africa
    Football     Australia
    Football   Netherlands
    Football   New Zealand
    Football       England
    Football  South Africa
    Football     Australia
    Football   Netherlands
    Football   New Zealand
Hoc Hockey           India
    Hockey        Pakistan
    Hockey     South Korea
    Hockey         England
    Hockey           India
    Hockey     New Zealand

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=392177&siteId=1