将一个熊猫群按对象转换为DataFrame

时间:2022-01-05 20:55:07

I'm starting with input data like this

我从像这样的输入数据开始

df1 = pandas.DataFrame( { 
    "Name" : ["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"] , 
    "City" : ["Seattle", "Seattle", "Portland", "Seattle", "Seattle", "Portland"] } )

Which when printed appears as this:

当印刷出来的时候:

   City     Name
0   Seattle    Alice
1   Seattle      Bob
2  Portland  Mallory
3   Seattle  Mallory
4   Seattle      Bob
5  Portland  Mallory

Grouping is simple enough:

分组很简单:

g1 = df1.groupby( [ "Name", "City"] ).count()

and printing yields a GroupBy object:

打印产生GroupBy对象:

                  City  Name
Name    City
Alice   Seattle      1     1
Bob     Seattle      2     2
Mallory Portland     2     2
        Seattle      1     1

But what I want eventually is another DataFrame object that contains all the rows in the GroupBy object. In other words I want to get the following result:

但是我最终想要的是另一个DataFrame对象,它包含GroupBy对象中的所有行。换句话说,我想得到以下结果:

                  City  Name
Name    City
Alice   Seattle      1     1
Bob     Seattle      2     2
Mallory Portland     2     2
Mallory Seattle      1     1

I can't quite see how to accomplish this in the pandas documentation. Any hints would be welcome.

我无法在熊猫文件中看到如何实现这一点。任何提示都是受欢迎的。

6 个解决方案

#1


345  

g1 here is a DataFrame. It has a hierarchical index, though:

g1是一个DataFrame。不过,它有一个分级索引:

In [19]: type(g1)
Out[19]: pandas.core.frame.DataFrame

In [20]: g1.index
Out[20]: 
MultiIndex([('Alice', 'Seattle'), ('Bob', 'Seattle'), ('Mallory', 'Portland'),
       ('Mallory', 'Seattle')], dtype=object)

Perhaps you want something like this?

也许你想要这样的东西?

In [21]: g1.add_suffix('_Count').reset_index()
Out[21]: 
      Name      City  City_Count  Name_Count
0    Alice   Seattle           1           1
1      Bob   Seattle           2           2
2  Mallory  Portland           2           2
3  Mallory   Seattle           1           1

Or something like:

或者类似的:

In [36]: DataFrame({'count' : df1.groupby( [ "Name", "City"] ).size()}).reset_index()
Out[36]: 
      Name      City  count
0    Alice   Seattle      1
1      Bob   Seattle      2
2  Mallory  Portland      2
3  Mallory   Seattle      1

#2


72  

I want to little bit change answer by Wes, because version 0.16.2 need set as_index=False. If you don't set it, you get empty dataframe.

我想稍微改变一下Wes的答案,因为版本0.16.2需要设置as_index=False。如果不设置,就会得到空的dataframe。

Source:

来源:

Aggregation functions will not return the groups that you are aggregating over if they are named columns, when as_index=True, the default. The grouped columns will be the indices of the returned object.

如果被命名为列,那么聚合函数将不会返回正在聚合的组,而as_index=True则是默认值。分组列将是返回对象的索引。

Passing as_index=False will return the groups that you are aggregating over, if they are named columns.

传递as_index=False将返回您正在聚合的组,如果它们被命名为列。

Aggregating functions are ones that reduce the dimension of the returned objects, for example: mean, sum, size, count, std, var, sem, describe, first, last, nth, min, max. This is what happens when you do for example DataFrame.sum() and get back a Series.

聚合函数是减少返回对象的维数的函数,例如:平均数、sum、size、count、std、var、sem、describe、first、last、nth、min、max。这是当您使用DataFrame.sum()并返回一个系列时所发生的情况。

nth can act as a reducer or a filter, see here.

n可以作为还原剂或过滤器,请看这里。

import pandas as pd

df1 = pd.DataFrame({"Name":["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"],
                    "City":["Seattle","Seattle","Portland","Seattle","Seattle","Portland"]})
print df1
#
#       City     Name
#0   Seattle    Alice
#1   Seattle      Bob
#2  Portland  Mallory
#3   Seattle  Mallory
#4   Seattle      Bob
#5  Portland  Mallory
#
g1 = df1.groupby(["Name", "City"], as_index=False).count()
print g1
#
#                  City  Name
#Name    City
#Alice   Seattle      1     1
#Bob     Seattle      2     2
#Mallory Portland     2     2
#        Seattle      1     1
#

EDIT:

编辑:

In version 0.17.1 and later you can use subset in count and reset_index with parameter name in size:

在0.17.1版本和以后的版本中,您可以使用count和reset_index中的子集,参数名称的大小为:

print df1.groupby(["Name", "City"], as_index=False ).count()
#IndexError: list index out of range

print df1.groupby(["Name", "City"]).count()
#Empty DataFrame
#Columns: []
#Index: [(Alice, Seattle), (Bob, Seattle), (Mallory, Portland), (Mallory, Seattle)]

print df1.groupby(["Name", "City"])[['Name','City']].count()
#                  Name  City
#Name    City                
#Alice   Seattle      1     1
#Bob     Seattle      2     2
#Mallory Portland     2     2
#        Seattle      1     1

print df1.groupby(["Name", "City"]).size().reset_index(name='count')
#      Name      City  count
#0    Alice   Seattle      1
#1      Bob   Seattle      2
#2  Mallory  Portland      2
#3  Mallory   Seattle      1

The difference between count and size is that size counts NaN values while count does not.

计数和大小之间的区别是,大小计算NaN值,而count不计算。

#3


7  

Simply, this should do the task:

简单地说,这应该完成以下任务:

import pandas as pd

grouped_df = df1.groupby( [ "Name", "City"] )

pd.DataFrame(grouped_df.size().reset_index(name = "Group_Count"))

Here, grouped_df.size() pulls up the unique groupby count, and reset_index() method resets the name of the column you want it to be. Finally, the pandas Dataframe() function is called upon to create DataFrame object.

在这里,grouped_df.size()通过计数提取惟一的groupby,然后reset_index()方法重置您想要的列的名称。最后,调用panda()函数来创建Dataframe对象。

#4


5  

I found this worked for me.

我发现这对我很有效。

import numpy as np
import pandas as pd

df1 = pd.DataFrame({ 
    "Name" : ["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"] , 
    "City" : ["Seattle", "Seattle", "Portland", "Seattle", "Seattle", "Portland"]})

df1['City_count'] = 1
df1['Name_count'] = 1

df1.groupby(['Name', 'City'], as_index=False).count()

#5


4  

Maybe I misunderstand the question but if you want to convert the groupby back to a dataframe you can use .to_frame(). I wanted to reset the index when I did this so I included that part as well.

也许我误解了这个问题,但是如果您想将groupby转换为dataframe,可以使用.to_frame()。我想在做这个的时候重置索引所以我也包括了这一部分。

example code unrelated to question

与问题无关的示例代码

df = df['TIME'].groupby(df['Name']).min()
df = df.to_frame()
df = df.reset_index(level=['Name',"TIME"])

#6


2  

I have aggregated with Qty wise data and store to dataframe

我已经与Qty wise数据聚合并存储到dataframe

almo_grp_data = pd.DataFrame({'Qty_cnt' :
almo_slt_models_data.groupby( ['orderDate','Item','State Abv']
          )['Qty'].sum()}).reset_index()

#1


345  

g1 here is a DataFrame. It has a hierarchical index, though:

g1是一个DataFrame。不过,它有一个分级索引:

In [19]: type(g1)
Out[19]: pandas.core.frame.DataFrame

In [20]: g1.index
Out[20]: 
MultiIndex([('Alice', 'Seattle'), ('Bob', 'Seattle'), ('Mallory', 'Portland'),
       ('Mallory', 'Seattle')], dtype=object)

Perhaps you want something like this?

也许你想要这样的东西?

In [21]: g1.add_suffix('_Count').reset_index()
Out[21]: 
      Name      City  City_Count  Name_Count
0    Alice   Seattle           1           1
1      Bob   Seattle           2           2
2  Mallory  Portland           2           2
3  Mallory   Seattle           1           1

Or something like:

或者类似的:

In [36]: DataFrame({'count' : df1.groupby( [ "Name", "City"] ).size()}).reset_index()
Out[36]: 
      Name      City  count
0    Alice   Seattle      1
1      Bob   Seattle      2
2  Mallory  Portland      2
3  Mallory   Seattle      1

#2


72  

I want to little bit change answer by Wes, because version 0.16.2 need set as_index=False. If you don't set it, you get empty dataframe.

我想稍微改变一下Wes的答案,因为版本0.16.2需要设置as_index=False。如果不设置,就会得到空的dataframe。

Source:

来源:

Aggregation functions will not return the groups that you are aggregating over if they are named columns, when as_index=True, the default. The grouped columns will be the indices of the returned object.

如果被命名为列,那么聚合函数将不会返回正在聚合的组,而as_index=True则是默认值。分组列将是返回对象的索引。

Passing as_index=False will return the groups that you are aggregating over, if they are named columns.

传递as_index=False将返回您正在聚合的组,如果它们被命名为列。

Aggregating functions are ones that reduce the dimension of the returned objects, for example: mean, sum, size, count, std, var, sem, describe, first, last, nth, min, max. This is what happens when you do for example DataFrame.sum() and get back a Series.

聚合函数是减少返回对象的维数的函数,例如:平均数、sum、size、count、std、var、sem、describe、first、last、nth、min、max。这是当您使用DataFrame.sum()并返回一个系列时所发生的情况。

nth can act as a reducer or a filter, see here.

n可以作为还原剂或过滤器,请看这里。

import pandas as pd

df1 = pd.DataFrame({"Name":["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"],
                    "City":["Seattle","Seattle","Portland","Seattle","Seattle","Portland"]})
print df1
#
#       City     Name
#0   Seattle    Alice
#1   Seattle      Bob
#2  Portland  Mallory
#3   Seattle  Mallory
#4   Seattle      Bob
#5  Portland  Mallory
#
g1 = df1.groupby(["Name", "City"], as_index=False).count()
print g1
#
#                  City  Name
#Name    City
#Alice   Seattle      1     1
#Bob     Seattle      2     2
#Mallory Portland     2     2
#        Seattle      1     1
#

EDIT:

编辑:

In version 0.17.1 and later you can use subset in count and reset_index with parameter name in size:

在0.17.1版本和以后的版本中,您可以使用count和reset_index中的子集,参数名称的大小为:

print df1.groupby(["Name", "City"], as_index=False ).count()
#IndexError: list index out of range

print df1.groupby(["Name", "City"]).count()
#Empty DataFrame
#Columns: []
#Index: [(Alice, Seattle), (Bob, Seattle), (Mallory, Portland), (Mallory, Seattle)]

print df1.groupby(["Name", "City"])[['Name','City']].count()
#                  Name  City
#Name    City                
#Alice   Seattle      1     1
#Bob     Seattle      2     2
#Mallory Portland     2     2
#        Seattle      1     1

print df1.groupby(["Name", "City"]).size().reset_index(name='count')
#      Name      City  count
#0    Alice   Seattle      1
#1      Bob   Seattle      2
#2  Mallory  Portland      2
#3  Mallory   Seattle      1

The difference between count and size is that size counts NaN values while count does not.

计数和大小之间的区别是,大小计算NaN值,而count不计算。

#3


7  

Simply, this should do the task:

简单地说,这应该完成以下任务:

import pandas as pd

grouped_df = df1.groupby( [ "Name", "City"] )

pd.DataFrame(grouped_df.size().reset_index(name = "Group_Count"))

Here, grouped_df.size() pulls up the unique groupby count, and reset_index() method resets the name of the column you want it to be. Finally, the pandas Dataframe() function is called upon to create DataFrame object.

在这里,grouped_df.size()通过计数提取惟一的groupby,然后reset_index()方法重置您想要的列的名称。最后,调用panda()函数来创建Dataframe对象。

#4


5  

I found this worked for me.

我发现这对我很有效。

import numpy as np
import pandas as pd

df1 = pd.DataFrame({ 
    "Name" : ["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"] , 
    "City" : ["Seattle", "Seattle", "Portland", "Seattle", "Seattle", "Portland"]})

df1['City_count'] = 1
df1['Name_count'] = 1

df1.groupby(['Name', 'City'], as_index=False).count()

#5


4  

Maybe I misunderstand the question but if you want to convert the groupby back to a dataframe you can use .to_frame(). I wanted to reset the index when I did this so I included that part as well.

也许我误解了这个问题,但是如果您想将groupby转换为dataframe,可以使用.to_frame()。我想在做这个的时候重置索引所以我也包括了这一部分。

example code unrelated to question

与问题无关的示例代码

df = df['TIME'].groupby(df['Name']).min()
df = df.to_frame()
df = df.reset_index(level=['Name',"TIME"])

#6


2  

I have aggregated with Qty wise data and store to dataframe

我已经与Qty wise数据聚合并存储到dataframe

almo_grp_data = pd.DataFrame({'Qty_cnt' :
almo_slt_models_data.groupby( ['orderDate','Item','State Abv']
          )['Qty'].sum()}).reset_index()