Python Pandas Groupby Tutorial

https://www.marsja.se/python-pandas-groupby-tutorial-examples/

In this Pandas group by we are going to learn how to organize Pandas dataframes by groups. More specifically, we are going to learn how to group by one and multiple columns. Furthermore, we are going to learn how calculate some basics summary statistics (e.g., mean, median), convert Pandas groupby to dataframe, calculate the percentage of observations in each group, and many more useful things.

First of all we are going to import pandas as pd, and read a CSV file, using the read_csv method, to a dataframe. In the example below, we use index_col=0 because the first row in the dataset is the index column.

<textarea class="crayon-plain print-no" data-settings="dblclick" readonly="readonly" style="-moz-tab-size: 4; font-size: 12px !important; line-height: 15px !important; z-index: 0; opacity: 0; overflow: hidden;" wrap="soft">import pandas as pd data_url = 'http://vincentarelbundock.github.io/Rdatasets/csv/carData/Salaries.csv' df = pd.read_csv(data_url, index_col=0) df.head()</textarea>

|

1

2

3

4

5

6

|

import pandas as pd

data_url = 'http://vincentarelbundock.github.io/Rdatasets/csv/carData/Salaries.csv'

df = pd.read_csv(data_url, index_col=0)

df.head()

|

image

We used Pandas head to se the first 5 rows of our dataframe. In the image above we can see that we have, at least, three variables that we can group our data by. That is, we can group our data by “rank”, “discipline”, and “sex”. Of course, we could also group it by yrs.since.phd or yrs.service but it may be a lot of groups. As previously mentioned we are going to use Pandas groupby to group a dataframe based on one, two, three, or more columns.

Data can be loaded from other file formats as well (e.g., Excel, HTML, JSON):

Python Pandas Groupby Example

We are starting with the simplest example; grouping by one column. In the Pandas groupby example below we are going to group by the column “rank”.

There are many different methods that we can use on Pandas groupby objects (and Pandas dataframe objects). All available methods on a Python object can be found using this code:

<textarea class="crayon-plain print-no" data-settings="dblclick" readonly="readonly" style="-moz-tab-size: 4; font-size: 12px !important; line-height: 15px !important; z-index: 0; opacity: 0; overflow: hidden;" wrap="soft">import IPython # Grouping by one factor df_rank = df.groupby('rank') # Getting all methods from the groupby object: meth = [method_name for method_name in dir(df_rank) if callable(getattr(df_rank, method_name)) & ~method_name.startswith('_')] # Printing the result print(IPython.utils.text.columnize(meth))</textarea>

|

1

2

3

4

5

6

7

8

9

10

11

|

import IPython

Grouping by one factor

df_rank = df.groupby('rank')

Getting all methods from the groupby object:

meth = [method_name for method_name in dir(df_rank)

if callable(getattr(df_rank, method_name)) & ~method_name.startswith('_')]

Printing the result

print(IPython.utils.text.columnize(meth))

|

image

Note, that in the code example above we also import IPython to print the list in columns. In the following examples we are going to use some of these methods. First, we can print out the groups by using the groups method to get a dictionary of groups:

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 10,080评论 0赞 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 8,235评论 0赞 10
  • 图片发自简书App 涓志涌勤泉, 涓心向大川。 清溪怀韧玉, 流海亦当然。 (平水韵) 后记:在《简书》认识简...
    常将三侠阅读 2,260评论 20赞 26
  • 那为你绽放的景色 剩一个人赏 为你保...
    陌心人mo阅读 228评论 0赞 0
  • 在写下这篇文章的时候,其实我看着这空白页面很久,因为我好像组织不了语言。 怎么说呢,就是那种感觉,想说很多话,却怎...
    欢脱故事者JiA阅读 581评论 0赞 0

友情链接更多精彩内容