简体   繁体   English

使用groupby键作为pandas数据帧的索引

[英]Use groupby keys as indexes of pandas dataframe

I have a following pandas dataframe df : 我有一个以下的pandas数据帧df

                    % Renewable  Energy Supply
Country                                       
China                 19.754910   1.271910e+11
United States         11.570980   9.083800e+10
Japan                 10.232820   1.898400e+10
United Kingdom        10.600470   7.920000e+09
Russian Federation    17.288680   3.070900e+10
Canada                61.945430   1.043100e+10
Germany               17.901530   1.326100e+10
India                 14.969080   3.319500e+10
France                17.020280   1.059700e+10
South Korea            2.279353   1.100700e+10
Italy                 33.667230   6.530000e+09
Spain                 37.968590   4.923000e+09
Iran                   5.707721   9.172000e+09
Australia             11.810810   5.386000e+09
Brazil                69.648030   1.214900e+10

I am grouping this dataframe using the Continents each country belongs to and also using the bins obtained by using pd.cut on the column % Renewable : 我使用每个国家/地区所属的大陆对此数据框进行分组,并使用在% Renewable列上使用pd.cut获得的分箱:

out, bins = pd.cut(Top15['% Renewable'].values, bins = 5, retbins = True)
grp = Top15.groupby(by = [ContinentDict, out])

where, 哪里,

ContinentDict  = {'China':'Asia', 
              'United States':'North America', 
              'Japan':'Asia', 
              'United Kingdom':'Europe', 
              'Russian Federation':'Europe', 
              'Canada':'North America', 
              'Germany':'Europe', 
              'India':'Asia',
              'France':'Europe', 
              'South Korea':'Asia', 
              'Italy':'Europe', 
              'Spain':'Europe', 
              'Iran':'Asia',
              'Australia':'Australia', 
              'Brazil':'South America'}   

Now, I want to create a new dataframe with the same columns as df and another column given by 'Country'. 现在,我想创建一个新的数据框,其列与df相同,另一列由'Country'创建。 The indexes of this new dataframe should be given by groupby objects keys hierarchically ('Continent', 'out'). 这个新数据帧的索引应该由groupby对象键分层给出('Continent','out')。 After hours of trial, I see no way to do this. 经过几个小时的试验,我认为没办法做到这一点。 Any ideas? 有任何想法吗?

You can create a multi-index from continent and cut and assign it back to your data frame: 您可以从continent创建多索引并cut并将其分配回数据框:

out, bins = pd.cut(Top15['% Renewable'].values, bins = 5, retbins = True)
con = Top15.index.to_series().map(ContinentDict).values

Top15.reset_index(inplace=True)
Top15.index = pd.MultiIndex.from_arrays([con, out])
Top15

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM