关于python:将pandas groupby对象转换为数据帧

Converting a Pandas GroupBy object to DataFrame

我从这样的输入数据开始

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

打印时显示如下:

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

分组非常简单:

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

打印生成一个GroupBy对象:

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

但我最终想要的是另一个包含groupby对象中所有行的数据帧对象。换句话说,我希望得到以下结果:

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

我不太明白如何在熊猫文档中完成这一点。任何提示都是受欢迎的。


g1here is a dataframe。它已经在分级指数,虽然:P></

1
2
3
4
5
6
7
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)

也许你想要的东西吗?P></

1
2
3
4
5
6
7
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:P></

1
2
3
4
5
6
7
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


我的答案slightly change the given by韦斯,因为……as_index=False0.16.2版本。如果你不把它安集,你dataframe Empty。P></

源码:P></

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.

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

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.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
#

编辑:P></

在以后的版本,你可以使用0.17.1subsetcountreset_index参数:在sizenamewithP></

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

差分countbetween the sizeis that size布尔值countdoes not while counts南。P></


简单的任务,this should do the:P></

1
2
3
4
5
import pandas as pd

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

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

在这里,grouped _ df.size(弹出)pulls茶独特的GroupBy count,和复位_ index()方法resets the name of the column to be你想要它。最后,《大熊猫dataframe is called to function()创建的对象dataframe河畔。P></


我的问题,但如果你想misunderstand convert to the back to a dataframe GroupBy(你可以使用.to _帧)。复位所有想当我知道this the index that included作为兼职的好。P></

队列的问题unrelated example toP></

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

found this for我的挤压。P></

1
2
3
4
5
6
7
8
9
10
11
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()

解决方案:简单的内部可能是belowP></

1
df1.reset_index().groupby( ["Name","City"],as_index=False ).count()


我的日期和商店聚集的数量与dataframe wise toP></

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

这些解决方案的唯一的部分工作都为我做aggregations因为我多。这里是我的grouped样品输出模式(我想dataframe:convert to aP></

Groupby OutputP></

我希望更多的_复位模式提供比count index(),在wrote method for the above手册进入dataframe图像转换。This is not the most明白的语言/大熊猫的方式做this as is of EN和显那么详细,但它是所有我需要的。基本上,使用复位_指数(the method to start a explained above)",然后scaffolding"dataframe环一组,pairings grouped dataframe茶茶,茶retrieve指数的计算,对ungrouped dataframe做你的茶,和集值dataframe聚集在你的新。P></

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
df_grouped = df[['Salary Basis', 'Job Title', 'Hourly Rate', 'Male Count', 'Female Count']]
df_grouped = df_grouped.groupby(['Salary Basis', 'Job Title'], as_index=False)

# Grouped gives us the indices we want for each grouping
# We cannot convert a groupedby object back to a dataframe, so we need to do it manually
# Create a new dataframe to work against
df_aggregated = df_grouped.size().to_frame('Total Count').reset_index()
df_aggregated['Male Count'] = 0
df_aggregated['Female Count'] = 0
df_aggregated['Job Rate'] = 0

def manualAggregations(indices_array):
    temp_df = df.iloc[indices_array]
    return {
        'Male Count': temp_df['Male Count'].sum(),
        'Female Count': temp_df['Female Count'].sum(),
        'Job Rate': temp_df['Hourly Rate'].max()
    }

for name, group in df_grouped:
    ix = df_grouped.indices[name]
    calcDict = manualAggregations(ix)

    for key in calcDict:
        #Salary Basis, Job Title
        columns = list(name)
        df_aggregated.loc[(df_aggregated['Salary Basis'] == columns[0]) &
                          (df_aggregated['Job Title'] == columns[1]), key] = calcDict[key]

如果你在字典不是茶事,茶是被用inline for循环:P></

1
2
    df_aggregated['Male Count'].loc[(df_aggregated['Salary Basis'] == columns[0]) &
                                (df_aggregated['Job Title'] == columns[1])] = df['Male Count'].iloc[ix].sum()


is the key to the复位使用_ index()方法。P></

使用:P></

1
2
3
4
5
6
7
import pandas

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

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

现在你有你的新dataframe G1期:P></

result dataframeP></