Skip to content

Commit 5e1cb8c

Browse files
committed
Add html repr for groupby dataframe and series
1 parent 7d0ee96 commit 5e1cb8c

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

pandas/core/groupby/groupby.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class providing the base-class of operations.
3333
import numpy as np
3434

3535
from pandas._config.config import option_context
36+
from pandas._config import get_option
3637

3738
from pandas._libs import Timestamp
3839
import pandas._libs.groupby as libgroupby
@@ -542,6 +543,17 @@ def __repr__(self) -> str:
542543
# TODO: Better repr for GroupBy object
543544
return object.__repr__(self)
544545

546+
def _repr_html_(self) -> str:
547+
html_text = ""
548+
for idx, df_group in self:
549+
if not hasattr(df_group, "to_html"):
550+
df_group = df_group.to_frame()
551+
html_text += f"<H3>Group Key: {idx}<H3/>"
552+
html_text += df_group.to_html(
553+
max_rows=get_option("display.max_rows") // self.ngroups
554+
)
555+
return html_text
556+
545557
def _assure_grouper(self):
546558
"""
547559
We create the grouper on instantiation sub-classes may have a

0 commit comments

Comments
 (0)