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

我有一个数据框架:

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

当按“A”分组后打印时,我有以下内容:

print(df.groupby('A'))

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

如何打印分组的数据框架?

如果我这样做:

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

我获得的数据帧好像它没有分组:

             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

我期待的是:

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

当前回答

在Jupyter Notebook中,如果您执行以下操作,它将打印对象的漂亮分组版本。apply方法有助于创建多索引数据框架。

by = 'A'  # groupby 'by' argument
df.groupby(by).apply(lambda a: a[:])

输出:

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

如果您希望by列不出现在输出中,只需删除该列,如下所示。

df.groupby(by).apply(lambda a: a.drop(by, axis=1)[:])

输出:

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

在这里,我不确定为什么.iloc[:]不能在结尾工作而不是[:]。因此,如果将来由于更新(或当前)而出现一些问题,.iloc[:len(a)]也可以工作。

其他回答

打印所有(或任意多行)的分组df:

import pandas as pd
pd.set_option('display.max_rows', 500)

grouped_df = df.group(['var1', 'var2'])
print(grouped_df)

只是做的事:

grouped_df = df.groupby('A')

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

弃用通知:ix在0.20.0中已弃用

这也是可行的,

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

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

对于选择性键分组:使用gb.keys()将您想要的键插入到key_list_from_gb,在下面:

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")

我发现了一个棘手的方法,只是为了头脑风暴,见代码:

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)

输出:

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

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

为Groupby对象分配一个变量并使用.first()方法。例子:

 a = df_apps_clean[['App', 'Installs']].groupby('Installs')
 a.first()  <----

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

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