I am looping through the groups of a pandas groupby
object to print the (sub)dataframe for each group. The headings are printed for each group. Here are some of the (sub)dataframes, with column headings “MMSI” and “ShipName”:
MMSI ShipName
15468 109080345 OYANES 3 [19%]
46643 109080345 OYANES 3 [18%]
MMSI ShipName
19931 109080342 OYANES 2 [83%]
48853 109080342 OYANES 2 [82%]
MMSI ShipName
45236 109050943 SVARTHAV 2 [11%]
48431 109050943 SVARTHAV 2 [14%]
MMSI ShipName
21596 109050904 MR:N2FE [88%]
49665 109050904 MR:N2FE [87%]
MMSI ShipName
13523 941500907 MIKKELSEN B 5 [75%]
45711 941500907 MIKKELSEN B 5 [74%]
Web searching shows that pandas.io.formats.style.Styler.hide_columns
can be used to suppress the headings. I am using Python 3.9, in which hide_columns
is not recognized. However, dir(pd.io.formats.style.Styler)
shows a hide
method, for which the doc string gives this first example:
>>> df = pd.DataFrame([[1,2], [3,4], [5,6]], index=["a", "b", "c"])
>>> df.style.hide(["a", "b"]) # doctest: +SKIP
0 1
c 5 6
When I try hide()
and variations thereof, all I get is an address to the resulting Styler
object:
>>> df.style.hide(["a", "b"]) # doctest: +SKIP
<pandas.io.formats.style.Styler at 0x243baeb1760>
>>> df.style.hide(axis='columns') # /a/69111895
<pandas.io.formats.style.Styler at 0x243baeb17c0>
>>> df.style.hide() # Desparate random trial & error
<pandas.io.formats.style.Styler at 0x243baeb1520>
What could cause my result to differ from the doc string? How can I properly use the Styler
object to get the dataframe printed without column headings?