Note that there are some explanatory texts on larger screens.

plurals
  1. PORenaming Column Names in Pandas Groupby function
    text
    copied!<p>1). I have a following example dataset: </p> <pre><code>&gt;&gt;&gt; df ID Region count 0 100 Asia 2 1 101 Europe 3 2 102 US 1 3 103 Africa 5 4 100 Russia 5 5 101 Australia 7 6 102 US 8 7 104 Asia 10 8 105 Europe 11 9 110 Africa 23 </code></pre> <p>I wanted to group the observations of this dataset by ID an Region and summing the count for each group. So I used something like this:</p> <pre><code>&gt;&gt;&gt; print(df.groupby(['ID','Region'],as_index=False).count.sum()) ID Region count 0 100 Asia 2 1 100 Russia 5 2 101 Australia 7 3 101 Europe 3 4 102 US 9 5 103 Africa 5 6 104 Asia 10 7 105 Europe 11 8 110 Africa 23 </code></pre> <p>On using as_index=False I am able to get a "Sql-Like" output. My problem is that I am unable to rename the aggregate variable count here. So in Sql if wanted to do the above thing I would do something like this:</p> <pre><code>select ID, Region, sum(count) as Total_Numbers from df group by ID,Region order by ID, Region </code></pre> <p>As we see, it's very easy for me to rename the aggregate variable 'count' to Total_Numbers in SQL. I wanted to do the same thing in Pandas but unable to find such option in groupby function. Can somebody help?</p> <p>2). The second question and more of an observation is that is it possible to use directly the column names in Pandas dataframe function witout enclosing them inside quotes? I understand that the variable names are string, so has to be inside quotes, but I see if use outside dataframe function and as an attribute we don't require them to be inside quotes. Like df.ID.sum() etc. It's only when we use it in a DataFrame function like df.sort() or df.groupby we have to use it inside quotes. This is actually a bit of pain as in SQL or in SAS or other language we simply use the variable name without quoting them. Any suggestion on this? </p> <p>Kindly suggest on the above two points(1st one main, 2nd more of an opinion). </p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload