Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide pandas column headings in a terminal window to save space and reduce cognitive noise

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') # https://stackoverflow.com/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?

I am using pandas 2.0.3 with Spyder 5.4.3.

like image 930
user2153235 Avatar asked Feb 03 '26 15:02

user2153235


1 Answers

I don’t know about Styler, but you can print the data without headers like this.

import pandas as pd

df = pd.read_csv(r"/home/bera/GIS/data/Pandas_testdata/flights.csv")

for month, subframe in df.groupby("month"):
    print(subframe.to_string(header=False))
    print("\n")

Output:

3    1949  April  129
15   1950  April  135
27   1951  April  163
39   1952  April  181
51   1953  April  235
63   1954  April  227
75   1955  April  269
87   1956  April  313
99   1957  April  348
111  1958  April  348
123  1959  April  396
135  1960  April  461


7    1949  August  148
19   1950  August  170
31   1951  August  199
43   1952  August  242
55   1953  August  272
67   1954  August  293
79   1955  August  347
91   1956  August  405
103  1957  August  467
115  1958  August  505
127  1959  August  559
139  1960  August  606
like image 136
BERA Avatar answered Feb 06 '26 05:02

BERA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!