📜  如何将数据帧行分组到 Pandas Groupby 中的列表中?

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

如何将数据帧行分组到 Pandas Groupby 中的列表中?

假设您有一个由 2 列组成的 Pandas DataFrame,我们想对这些列进行分组。在本文中,我们将讨论相同的内容。首先,让我们创建数据框。

Python3
# importing pandas as pd
import pandas as pd
  
  
# Create the data frame
df = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C',
                               'C', 'B', 'D', 'D', 'A'],
                   'column2': [5, 10, 15, 20, 25, 30, 
                             35, 40, 45, 50]})
  
# Print the dataframe
df


Python3
# importing pandas as pd
import pandas as pd
  
  
# Create the data frame
df = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C',
                               'C', 'B', 'D', 'D', 'A'],
                   'column2': [5, 10, 15, 20, 25, 30, 
                             35, 40, 45, 50]})
  
  
# Use groupby method and apply
# method on the dataframe
df = df.groupby('column1')['column2'].apply(list)
  
# Print the dataframe again
df


Python3
# importing pandas as pd
import pandas as pd
  
  
# Create the dataframe
df = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C', 
                               'C', 'B', 'D', 'D', 'A'], 
                   'column2': [5, 10, 15, 20, 25, 30,
                               35, 40, 45, 50]})
  
# Use groupby method and agg method 
# with lambda function on the dataframe
df = df.groupby('column1').agg({'column2': lambda x: list(x)})
  
# Print the dataframe again
df


Python3
# importing pandas as pd
import pandas as pd
  
  
# Create the data frame
df = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C',
                               'C', 'B', 'D', 'D', 'A'],
                   'column2': [5, 10, 15, 20, 25, 30, 
                               35, 40, 45, 50]})
  
# Use groupby method and agg method 
# with list as argument on the dataframe
df = df.groupby('column1').agg(list)
  
df


Python3
# importing pandas as pd
import pandas as pd
  
  
# Create the data frame
df = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C', 
                               'C', 'B', 'D', 'D', 'A'], 
                   'column2': [5, 10, 15, 20, 25, 30,
                               35, 40, 45, 50]})
  
# Use groupby method and agg method with
# pd.Series.tolist as argument on the dataframe
df = df.groupby('column1').agg(pd.Series.tolist)
  
df


输出:

示例 #1:我们可以在第 1 列上使用 groupby() 方法,并应用该方法在每组 Pandas DataFrame 上应用一个列表。

蟒蛇3

# importing pandas as pd
import pandas as pd
  
  
# Create the data frame
df = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C',
                               'C', 'B', 'D', 'D', 'A'],
                   'column2': [5, 10, 15, 20, 25, 30, 
                             35, 40, 45, 50]})
  
  
# Use groupby method and apply
# method on the dataframe
df = df.groupby('column1')['column2'].apply(list)
  
# Print the dataframe again
df

输出:

示例#2:我们可以在第 1 列上使用 groupby() 方法和 agg() 方法在每组 Pandas DataFrame 上应用由 lambda函数组成的聚合。

蟒蛇3

# importing pandas as pd
import pandas as pd
  
  
# Create the dataframe
df = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C', 
                               'C', 'B', 'D', 'D', 'A'], 
                   'column2': [5, 10, 15, 20, 25, 30,
                               35, 40, 45, 50]})
  
# Use groupby method and agg method 
# with lambda function on the dataframe
df = df.groupby('column1').agg({'column2': lambda x: list(x)})
  
# Print the dataframe again
df

输出:

示例 #3:我们可以使用第 1 列的 groupby() 方法和 agg() 方法将聚合列表应用于每组 Pandas DataFrame。

蟒蛇3

# importing pandas as pd
import pandas as pd
  
  
# Create the data frame
df = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C',
                               'C', 'B', 'D', 'D', 'A'],
                   'column2': [5, 10, 15, 20, 25, 30, 
                               35, 40, 45, 50]})
  
# Use groupby method and agg method 
# with list as argument on the dataframe
df = df.groupby('column1').agg(list)
  
df

输出:

示例#4:我们可以通过将“pd.Series.tolist”作为参数传递来使用第 1 列的 groupby() 方法和 agg() 方法。

蟒蛇3

# importing pandas as pd
import pandas as pd
  
  
# Create the data frame
df = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C', 
                               'C', 'B', 'D', 'D', 'A'], 
                   'column2': [5, 10, 15, 20, 25, 30,
                               35, 40, 45, 50]})
  
# Use groupby method and agg method with
# pd.Series.tolist as argument on the dataframe
df = df.groupby('column1').agg(pd.Series.tolist)
  
df

输出: