📜  如何在Groupby熊猫之后重置索引?

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

如何在Groupby熊猫之后重置索引?

Python 的groupby()函数用途广泛。它用于根据一些标准(如均值、中值、value_counts等)将数据分组。为了在groupby()之后重置索引,我们将使用reset_index()函数。

以下是描述如何pandasgroupby()之后重置索引的各种示例

示例 1

Python3
# import required modules
import numpy as np
import pandas as pd
  
# creating dataframe
df = pd.DataFrame({'Subject': ['Physics', 
                               'Chemistry', 
                               'Maths'], 
                   'Marks': [4, 8, 5]})
  
# grouping the data on the basis of 
# subject and mean of marks.
df_grouped = df.groupby(['Subject']).mean()
  
# display dataset
df_grouped


Python3
# reset index
df_grouped.reset_index()


Python3
# import required modules
import pandas as pd
import numpy as np
  
# creating dataframe
df2 = pd.DataFrame({'Student': [1, 2, 3, 4, 1, 3, 2, 4, 1, 2, 4, 3],
                    'Amount': [
                   10, 20, 30, 40, 20, 60, 40, 80, 30, 60, 120, 90]})
  
# grouping the data
df2_group = df2.groupby(['Student'])
  
# grouped on the basis of students and
# with the value of count of amount
df2_grouped = df2_group['Amount'].value_counts()
  
# display dataset
print(df2_grouped)


Python3
# this will generate an error.
df2_grouped.reset_index()


Python3
# resetting index on the basis of count
df2_grouped.reset_index(name = 'count')


Python3
# import required modules
import numpy as np
import pandas as pd
  
# creating dataframe
df = pd.DataFrame({'Subject': ['B', 
                               'C', 
                               'A','D','C','B','A'], 
                   'Marks': [4, 8, 5,9,8,1,0]})
  
# grouping the data on the basis of 
# subject and mean of marks.
df_grouped = df.groupby(['Subject']).mean()
  
# display dataset
df_grouped
  
# reset index
df_grouped.reset_index()


输出:

数据分组后重置索引,使用reset_index(),是Python提供的一个函数,用于给数据添加索引。

蟒蛇3

# reset index
df_grouped.reset_index()

输出:

示例 2:

创建数据框。

蟒蛇3

# import required modules
import pandas as pd
import numpy as np
  
# creating dataframe
df2 = pd.DataFrame({'Student': [1, 2, 3, 4, 1, 3, 2, 4, 1, 2, 4, 3],
                    'Amount': [
                   10, 20, 30, 40, 20, 60, 40, 80, 30, 60, 120, 90]})
  
# grouping the data
df2_group = df2.groupby(['Student'])
  
# grouped on the basis of students and
# with the value of count of amount
df2_grouped = df2_group['Amount'].value_counts()
  
# display dataset
print(df2_grouped)

输出:

重置索引。这会给你一个错误。

蟒蛇3

# this will generate an error.
df2_grouped.reset_index()

输出:

命名reset_index()将分组并重置索引。

蟒蛇3

# resetting index on the basis of count
df2_grouped.reset_index(name = 'count')

输出:

示例 3

这是另一个描述如何在使用groupby() 后重置数据帧的示例。

蟒蛇3

# import required modules
import numpy as np
import pandas as pd
  
# creating dataframe
df = pd.DataFrame({'Subject': ['B', 
                               'C', 
                               'A','D','C','B','A'], 
                   'Marks': [4, 8, 5,9,8,1,0]})
  
# grouping the data on the basis of 
# subject and mean of marks.
df_grouped = df.groupby(['Subject']).mean()
  
# display dataset
df_grouped
  
# reset index
df_grouped.reset_index()

输出: