Python 如何打印groupby对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22691010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:25:15  来源:igfitidea点击:

How to print a groupby object

pythonpandas

提问by user3465658

I want to print the result of grouping with Pandas.

我想打印与 Pandas 分组的结果。

I have a 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?

如何打印分组的数据框?

If I do:

如果我做:

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

I obtain the dataframe as if it was not grouped:

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

             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

回答by Dan Allan

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 groupbyis separate kind of object, a GroupByobject. You must apply, transform, or filterto get back to a DataFrame or Series.

但是 groupby 操作实际上并不返回按组排序的 DataFrame。该.head()方法在这里有点误导——它只是一个方便的功能,让您重新检查df您分组的对象(在本例中为)。的结果groupby是不同种类的对象,一个GroupBy对象。您必须applytransform、 或filter才能返回到 DataFrame 或 Series。

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

如果您只想按 A 列中的值排序,则应使用df.sort('A').

回答by Surya

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.items():
    if key in key_list_from_gb:
        print(df.ix[values], "\n")

回答by Surya

Also, other simple alternative could be:

此外,其他简单的替代方法可能是:

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

回答by Swagath

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.

这给你一张整洁的桌子。

回答by Sumit Pokhrel

Another simple alternative:

另一个简单的选择:

for name_of_the_group, group in grouped_dataframe:
   print (name_of_the_group)
   print (group)

回答by mimoralea

Thanks to Surya for good insights. I'd clean up his solution and simply do:

感谢 Surya 提供了很好的见解。我会清理他的解决方案,然后简单地做:

for key, value in df.groupby('A'):
    print(key, value)

回答by Sheng Zhuang

I found a tricky way, just for brainstorm, see the code:

我找到了一个棘手的方法,只是为了头脑风暴,看代码:

df['a'] = df['A']  # create a shadow column for MultiIndexing
df.sort_values('A', inplace=True)
df.set_index(["A","a"], inplace=True)
print(df)

the output:

输出:

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

The pros is so easy to print, as it returns a dataframe, instead of Groupby Object. And the output looks nice. While the con is that it create a series of redundant data.

优点很容易打印,因为它返回一个数据帧,而不是 Groupby 对象。输出看起来不错。缺点是它会创建一系列冗余数据。

回答by praveen kumar

you cannot see the groupBy data directly by print statement but you can see by iterating over the group using for loop try this code to see the group by data

您无法通过打印语句直接查看 groupBy 数据,但可以通过使用 for 循环遍历组来查看此代码以查看按数据分组

group = df.groupby('A') #group variable contains groupby data
for A,A_df in group: # A is your column and A_df is group of one kind at a time
  print(A)
  print(A_df)

you will get an output after trying this as a groupby result

尝试将其作为 groupby 结果后,您将获得输出

I hope it helps

我希望它有帮助

回答by Elizabeth Orrico

Call list() on the GroupBy object

在 GroupBy 对象上调用 list()

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

gives you:

给你:

[('one',      A  B
0  one  0
1  one  1
5  one  5), ('three',        A  B
3  three  3
4  three  4), ('two',      A  B
2  two  2)]

回答by Deepanshu Mehta

In python 3

在蟒蛇 3

k = None
for name_of_the_group, group in dict(df_group):
    if(k != name_of_the_group):
        print ('\n', name_of_the_group)
        print('..........','\n')
    print (group)
    k = name_of_the_group

In more interactive way

以更具互动性的方式