如何按对象打印groupby

时间:2021-06-14 20:55:03

I want to print the result of grouping with Pandas.

我想打印与熊猫分组的结果。

I have a dataframe:

我有一个dataframe:

import pandas as pd
df = pd.DataFrame({'A': ['one', 'one', 'two', 'three', 'three', 'one'], 'B': range(6)})
print df

       A  B
0    one  0
1    one  1
2    two  2
3  three  3
4  three  4
5    one  5

When printing after grouping by 'A' I have the following:

按A分组打印时,我有:

print df.groupby('A')

<pandas.core.groupby.DataFrameGroupBy object at 0x05416E90>

How can I print the dataframe grouped?

如何打印分组的dataframe ?

If I do:

如果我做的事:

print df.groupby('A').head()

I obtain the dataframe as if it was not grouped:

我获得数据aframe,就好像它没有分组一样:

             A  B
A                
one   0    one  0
      1    one  1
two   2    two  2
three 3  three  3
      4  three  4
one   5    one  5

I was expecting something like:

我期待的是:

             A  B
A                
one   0    one  0
      1    one  1
      5    one  5
two   2    two  2
three 3  three  3
      4  three  4

5 个解决方案

#1


39  

Simply do:

只是做的事:

grouped_df = df.groupby('A')

for key, item in grouped_df:
    print grouped_df.get_group(key), "\n\n"

This also works,

这同样适用,

grouped_df = df.groupby('A')    
gb = grouped_df.groups

for key, values in gb.iteritems():
    print df.ix[values], "\n\n"

For selective key grouping: Insert the keys you want inside the key_list_from_gb, in following, using gb.keys(): For Example,

对于选择密钥分组:在key_list_from_gb中插入您想要的密钥,如下所示,使用gb.keys():例如,

gb = grouped_df.groups
gb.keys()

key_list_from_gb = [key1, key2, key3]

for key, values in gb.iteritems():
    if key in key_list_from_gb:
        print df.ix[values], "\n"

#2


18  

If you're simply looking for a way to display it, you could use describe():

如果您只是在寻找一种显示它的方式,您可以使用describe():

grp = df.groupby['colName']
grp.describe()

This gives you a neat table.

这个表格很简洁。

#3


6  

Also, other simple alternative could be:

此外,其他简单的选择可以是:

gb = df.groupby("A")
gb.count() # or,
gb.get_group(your_key)

#4


2  

I confirmed that the behavior of head() changes between version 0.12 and 0.13. That looks like a bug to me. I created an issue.

我确认head()的行为在版本0.12和0.13之间发生了变化。在我看来那是一个漏洞。我创建了一个问题。

But a groupby operation doesn't actually return a DataFrame sorted by group. The .head() method is a little misleading here -- it's just a convenience feature to let you re-examine the object (in this case, df) that you grouped. The result of groupby is separate kind of object, a GroupBy object. You must apply, transform, or filter to get back to a DataFrame or Series.

但是groupby操作实际上并不返回一个按组排序的DataFrame。这里的.head()方法有点误导——它只是一个方便的特性,可以让您重新检查分组的对象(在本例中为df)。groupby的结果是一种单独的对象,一种groupby对象。您必须应用、转换或过滤器,以返回到数据爆炸名或系列。

If all you wanted to do was sort by the values in columns A, you should use df.sort('A').

如果您要做的只是按列A中的值排序,那么您应该使用df.sort('A')。

#5


1  

How about to "list()" the object directly?

如何直接“list()”对象?

And then you can manipulate/print it as a normal data structure.

然后您可以将其作为一个普通的数据结构进行操作/打印。

#1


39  

Simply do:

只是做的事:

grouped_df = df.groupby('A')

for key, item in grouped_df:
    print grouped_df.get_group(key), "\n\n"

This also works,

这同样适用,

grouped_df = df.groupby('A')    
gb = grouped_df.groups

for key, values in gb.iteritems():
    print df.ix[values], "\n\n"

For selective key grouping: Insert the keys you want inside the key_list_from_gb, in following, using gb.keys(): For Example,

对于选择密钥分组:在key_list_from_gb中插入您想要的密钥,如下所示,使用gb.keys():例如,

gb = grouped_df.groups
gb.keys()

key_list_from_gb = [key1, key2, key3]

for key, values in gb.iteritems():
    if key in key_list_from_gb:
        print df.ix[values], "\n"

#2


18  

If you're simply looking for a way to display it, you could use describe():

如果您只是在寻找一种显示它的方式,您可以使用describe():

grp = df.groupby['colName']
grp.describe()

This gives you a neat table.

这个表格很简洁。

#3


6  

Also, other simple alternative could be:

此外,其他简单的选择可以是:

gb = df.groupby("A")
gb.count() # or,
gb.get_group(your_key)

#4


2  

I confirmed that the behavior of head() changes between version 0.12 and 0.13. That looks like a bug to me. I created an issue.

我确认head()的行为在版本0.12和0.13之间发生了变化。在我看来那是一个漏洞。我创建了一个问题。

But a groupby operation doesn't actually return a DataFrame sorted by group. The .head() method is a little misleading here -- it's just a convenience feature to let you re-examine the object (in this case, df) that you grouped. The result of groupby is separate kind of object, a GroupBy object. You must apply, transform, or filter to get back to a DataFrame or Series.

但是groupby操作实际上并不返回一个按组排序的DataFrame。这里的.head()方法有点误导——它只是一个方便的特性,可以让您重新检查分组的对象(在本例中为df)。groupby的结果是一种单独的对象,一种groupby对象。您必须应用、转换或过滤器,以返回到数据爆炸名或系列。

If all you wanted to do was sort by the values in columns A, you should use df.sort('A').

如果您要做的只是按列A中的值排序,那么您应该使用df.sort('A')。

#5


1  

How about to "list()" the object directly?

如何直接“list()”对象?

And then you can manipulate/print it as a normal data structure.

然后您可以将其作为一个普通的数据结构进行操作/打印。