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

时间:2016-12-23 16:28:17

标签: python python-3.x pandas group-by

我有以下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

我正在使用每个国家/地区所属的大陆对此数据框进行分组,并使用pd.cut% Renewable上使用out, bins = pd.cut(Top15['% Renewable'].values, bins = 5, retbins = True) grp = Top15.groupby(by = [ContinentDict, out]) 获得的分箱:

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'}   

其中,

df

现在,我想创建一个新的数据框,其列与<tiles-definitions> <!-- base styles --> <definition name="base.styles" > <put-list-attribute name="styles" cascade="true" > <add-attribute value="/view/common/jquery-ui-theme-base-v1.12.1.css" /> </put-list-attribute> </definition> <!-- base scripts --> <definition name="base.scripts" > <put-list-attribute name="scripts" cascade="true" > <add-attribute value="https://code.jquery.com/jquery-3.1.0.min.js" /> <add-attribute value="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" /> </put-list-attribute> </definition> <!-- base definition --> <definition name="base.definition" template="/WEB-INF/page/defaultLayout.jsp" > <put-attribute name="title" /> <put-attribute name="styles" value="base.styles.styles" cascade="true" /> <put-attribute name="header" value="/WEB-INF/page/common/header.jsp" /> <put-attribute name="body" /> <put-attribute name="scripts" value="base.scripts.scripts" cascade="true" /> <put-attribute name="footer" value="/WEB-INF/page/common/footer.jsp" /> </definition> </tiles-definitions> 相同,另一列由'Country'创建。这个新数据帧的索引应该由groupby对象键分层给出('Continent','out')。经过几个小时的试验,我认为没办法做到这一点。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可以从continentcut创建一个多索引,并将其分配回您的数据框:

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

enter image description here