Country %Renewable
China (15.753, 29.227]
United States (2.212, 15.753]
Japan (2.212, 15.753]
United Kingdom (2.212, 15.753]
Russian Federation (15.753, 29.227]
Canada (56.174, 69.648]
Germany (15.753, 29.227]
India (2.212, 15.753]
France (15.753, 29.227]
South Korea (2.212, 15.753]
Italy (29.227, 42.701]
Spain (29.227, 42.701]
Iran (2.212, 15.753]
Australia (2.212, 15.753]
Brazil (56.174, 69.648]
我有这个DATAFRAME,我想要一个具有多索引
'Continent'
-> '% renawable'
的序列,我知道我可以使用groupby,问题是我不确定如何正确执行submultindex,以及如何处理与分类结果系列示例:
Continent % Renewable Country
Europe (2.212, 15.753] ['France', 'United Kingdom', 'Russian Federation']
(15.753, 29.227] ['Germany', 'France']
(29.227, 42.701] ['Italy', 'Spain']
Asia (2.212, 15.753] ['India', 'South Korea', 'Iran', 'Japan', 'Iran']
(15.753, 29.227] ['China']
Oceania (2.212, 15.753] ['Australia']
North America (2.212, 15.753] ['United States']
(56.174, 69.648] ['Canada']
South America (56.174, 69.648] ['Brazil']
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'}
这是转换国家到大陆的命令
最佳答案
您可以使用replace
进行大洲映射,然后使用tolist
获取每个组的值列表:
In [53]: df['Continent'] = df.Country.replace(ContinentDict)
In [55]: df.groupby(['Continent', '%Renewable']).apply(lambda x: x.Country.tolist())
Out[55]:
Continent %Renewable
Asia (15.753, 29.227] [China]
(2.212, 15.753] [Japan, India, South Korea, Iran]
Australia (2.212, 15.753] [Australia]
Europe (15.753, 29.227] [Russian Federation, Germany, France]
(2.212, 15.753] [United Kingdom]
(29.227, 42.701] [Italy, Spain]
North America (2.212, 15.753] [United States]
(56.174, 69.648] [Canada]
South America (56.174, 69.648] [Brazil]
关于python - 生成缩进的多索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40333170/