p1r0
p1r0

Reputation: 438

how to hide index 0 of PANDAS and Jupyter

i want to hide de index 0 (rows) from my jupyter notebook:

display(df.style.set_properties(**{'text-align': 'left'})

the method i use is style in order to get it as an html and left align, where df is a simple PANDAS dataframe.

THANK YOU!

Upvotes: 3

Views: 1754

Answers (1)

piRSquared
piRSquared

Reputation: 294488

It is very unclear what you want. So I'm going to take a guess.

Consider the dataframe df

df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], list('ABC'), list('XY'))

Since simply "hiding" the first row is super easy as @chtonicdaemon pointed out:

df.iloc[1:]

   X  Y
B  3  4
C  5  6

I'm going to assume you wanted something different. What I've seen asked before is how do I not display the index? I'll first point at a previous answer of mine ColorChange

This is the function I'll use to apply my own css

def HTML_with_style(df, style=None, random_id=None):
    from IPython.display import HTML
    import numpy as np
    import re

    df_html = df.to_html()

    if random_id is None:
        random_id = 'id%d' % np.random.choice(np.arange(1000000))

    if style is None:
        style = """
        <style>
            table#{random_id} {{color: blue}}
        </style>
        """.format(random_id=random_id)
    else:
        new_style = []
        s = re.sub(r'</?style>', '', style).strip()
        for line in s.split('\n'):
                line = line.strip()
                if not re.match(r'^table', line):
                    line = re.sub(r'^', 'table ', line)
                new_style.append(line)
        new_style = ['<style>'] + new_style + ['</style>']

        style = re.sub(r'table(#\S+)?', 'table#%s' % random_id, '\n'.join(new_style))

    df_html = re.sub(r'<table', r'<table id=%s ' % random_id, df_html)

    return HTML(style + df_html)

And I'll call it with my own css to suppress the index

style = """
<style>
    table tr :first-child{display: none;}
</style>
"""

HTML_with_style(df, style)

enter image description here


This doesn't work for print(df)

Upvotes: 2

Related Questions