How To Print A Groupby Object
Solution 1:
Simply do:
grouped_df = df.groupby('A')
for key, item in grouped_df:
print(grouped_df.get_group(key), "\n\n")
Deprecation Notice:
ix
was deprecated in 0.20.0
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,
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")
Solution 2:
If you're simply looking for a way to display it, you could use describe():
grp = df.groupby['colName']
grp.describe()
This gives you a neat table.
Solution 3:
In addition to previous answers:
Taking your example,
df = pd.DataFrame({'A': ['one', 'one', 'two', 'three', 'three', 'one'], 'B': range(6)})
Then simple 1 line code
df.groupby('A').apply(print)
Solution 4:
In Jupyter Notebook, if you do the following, it prints a nice grouped version of the object. The apply
method helps in creation of a multiindex dataframe.
by = 'A'# groupby 'by' argument
df.groupby(by).apply(lambda a: a[:])
Output:
A B
A
one0one01one15one5
three 3 three 34 three 4
two 2 two 2
If you want the by
column(s) to not appear in the output, just drop the column(s), like so.
df.groupby(by).apply(lambda a: a.drop(by, axis=1)[:])
Output:
BA
one 001155
three 3344
two 22
Here, I am not sure as to why .iloc[:]
does not work instead of [:]
at the end. So, if there are some issues in future due to updates (or at present), .iloc[:len(a)]
also works.
Solution 5:
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.
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.
If all you wanted to do was sort by the values in columns A, you should use df.sort('A')
.
Post a Comment for "How To Print A Groupby Object"