📌  相关文章
📜  如何按组大小对分组的 Pandas 数据框进行排序?

📅  最后修改于: 2022-05-13 01:54:44.986000             🧑  作者: Mango

如何按组大小对分组的 Pandas 数据框进行排序?

在本文中,我们将讨论如何在 Pandas 中根据组大小对分组数据进行排序。

使用的功能

在这里,我们将通过列表将输入作为字典数据结构传递。

  • groupby(): groupby() 用于根据列值对数据进行分组。
  • size():用于获取数据框的大小。
  • 排序值(): 此函数按传递的列的升序或降序对数据框进行排序。

任务很简单,对于给定的数据帧,首先我们需要根据要求按任何列分组,然后根据列的大小排列分组的值。这里的大小是指一个值在列中出现的次数或其频率。

示例 1:

Python3
# importing pandas module for dataframe
import pandas as pd
  
# creating a dataframe with student
# name and subject
  
dataframe1 = pd.DataFrame({'student_name': ['bobby', 'ojaswi', 'gnanesh',
                                            'rohith', 'karthik', 'sudheer',
                                            'vani'],
                             
                           'subjects': ['dbms', 'python', 'dbms', 'oops',
                                        'oops', 'oops', 'dbms']})
  
# display dataframe
print(dataframe1)
  
# group the data  on subjects column based on
# size and sort in descending order
a = dataframe1.groupby('subjects').size().sort_values(ascending=False)
  
# group the data  on subjects column based on 
# size and sort in ascending order
b = dataframe1.groupby('subjects').size().sort_values(ascending=True)
  
print(a, b)


Python3
# importing pandas module for dataframe
import pandas as pd
  
# creating a dataframe with student name
# , subject and address
dataframe1 = pd.DataFrame({'student_name': ['bobby', 'ojaswi', 'gnanesh',
                                            'rohith', 'karthik', 'sudheer',
                                            'vani'],
                           'subjects': ['dbms', 'python', 'dbms', 'oops', 
                                        'oops', 'oops', 'dbms'],
                             
                           'address': ['ponnur', 'ponnur', 'hyd', 'tenali',
                                       'tenali', 'hyd', 'patna']})
  
# display dataframe
print(dataframe1)
  
# group the data  on address column based  
# on size and sort in descending order
a = dataframe1.groupby('address').size().sort_values(ascending=False)
  
# group the data  on address column based 
# on size and sort in ascending order
b = dataframe1.groupby('address').size().sort_values(ascending=True)
  
print(a, b)


Python3
# importing pandas module for dataframe
import pandas as pd
  
# creating a dataframe with student
# name , subject and address
dataframe1 = pd.DataFrame({'student_name': ['bobby', 'ojaswi', 'gnanesh',
                                            'rohith', 'karthik', 'sudheer',
                                            'vani'],
                             
                           'subjects': ['dbms', 'python', 'dbms', 'oops',
                                        'oops', 'oops', 'dbms'],
                             
                           'address': ['ponnur', 'ponnur', 'hyd', 'tenali',
                                       'tenali', 'hyd', 'patna']})
  
# display dataframe
print(dataframe1)
  
# group the data  on address and subjects
# column based on size and sort in descending
# order
a = dataframe1.groupby(['address', 'subjects']
                       ).size().sort_values(ascending=False)
  
# group the data  on address and subjects
# column based on size and sort in ascending
# order
b = dataframe1.groupby(['address', 'subjects']
                       ).size().sort_values(ascending=True)
  
print(a, b)


输出:

示例 2:

蟒蛇3

# importing pandas module for dataframe
import pandas as pd
  
# creating a dataframe with student name
# , subject and address
dataframe1 = pd.DataFrame({'student_name': ['bobby', 'ojaswi', 'gnanesh',
                                            'rohith', 'karthik', 'sudheer',
                                            'vani'],
                           'subjects': ['dbms', 'python', 'dbms', 'oops', 
                                        'oops', 'oops', 'dbms'],
                             
                           'address': ['ponnur', 'ponnur', 'hyd', 'tenali',
                                       'tenali', 'hyd', 'patna']})
  
# display dataframe
print(dataframe1)
  
# group the data  on address column based  
# on size and sort in descending order
a = dataframe1.groupby('address').size().sort_values(ascending=False)
  
# group the data  on address column based 
# on size and sort in ascending order
b = dataframe1.groupby('address').size().sort_values(ascending=True)
  
print(a, b)

输出:

我们还可以对多个列进行分组。语法保持不变,但我们需要在列表中传递多列并在 groupby() 中传递列表

句法:



示例 3:

蟒蛇3

# importing pandas module for dataframe
import pandas as pd
  
# creating a dataframe with student
# name , subject and address
dataframe1 = pd.DataFrame({'student_name': ['bobby', 'ojaswi', 'gnanesh',
                                            'rohith', 'karthik', 'sudheer',
                                            'vani'],
                             
                           'subjects': ['dbms', 'python', 'dbms', 'oops',
                                        'oops', 'oops', 'dbms'],
                             
                           'address': ['ponnur', 'ponnur', 'hyd', 'tenali',
                                       'tenali', 'hyd', 'patna']})
  
# display dataframe
print(dataframe1)
  
# group the data  on address and subjects
# column based on size and sort in descending
# order
a = dataframe1.groupby(['address', 'subjects']
                       ).size().sort_values(ascending=False)
  
# group the data  on address and subjects
# column based on size and sort in ascending
# order
b = dataframe1.groupby(['address', 'subjects']
                       ).size().sort_values(ascending=True)
  
print(a, b)

输出: