How can I sort by a column while binning another

30 Views Asked by At

I am trying to cut float values of each country while grouping them by continent. I had posted this previously but I got a little farther so I am going to put where I am at now. I wrote the following code:

groups =  pd.value_counts(pd.cut(renew['% Renewable'], 5))
groups = pd.DataFrame(groups).reset_index()

and I got the following DataFrame:

    % Renewable counts
0   (2.213, 15.754]     7
1   (15.754, 29.228]    4
2   (29.228, 42.702]    2
3   (56.176, 69.65]     2
4   (42.702, 56.176]    0

Now I would like to merge with the 'Continent' and create a Series with a multindex of 'Continent' and '% Renewable' and the counts as the Eeries value.

I would like to merge these so it is a count in a multindex Series grouped by continents with the counts of each country in each bin. An example would be like this:

Asia      (2.213, 15.754]       3
          (15.754, 29.228]      1
          (29.228, 42.702]      2
          (56.176, 69.65]       0
          (42.702, 56.176]      0
Europe    (2.213, 15.754]       2
          (15.754, 29.228]      2
          (29.228, 42.702]      0
          (56.176, 69.65]       1
          (42.702, 56.176]      0
>>....and so on

Here is a piece of the data frame I am getting the info from:

    Country Continent   % Renewable
0   China   Asia    (15.754, 29.228]
1   United States   North America   (2.213, 15.754]
2   Japan   Asia    (2.213, 15.754]
3   United Kingdom  Europe  (2.213, 15.754]
4   Russian Federation  Europe  (15.754, 29.228]
1

There are 1 best solutions below

5
stefandw On

Hard to say why that particular error happens without seeing the data, but first put all the variables you want to group by in the first argument, e.g.

groups = renew.groupby(['Continent', pd.cut(renew['% Renewable'], 5)])