How to add column delimiter to Pandas dataframe display

user15964 :

For example, define

df=pd.DataFrame(np.random.randint(0,10,(6,6)))
df

Which gives below display in Jupyter notebook

enter image description here

My question is that is it possible to add a column delimiter to the dataframe like

enter image description here


Thank you for all the answers, currently I use below custom functions

def css_border(x,pos):
        return ["border-left: 1px solid red" if i in pos else "border: 0px" for i, col in enumerate(x)]
def display_df_with_delimiter(df,pos):
    return df.style.apply(partial(css_border,pos=pos), axis=1)

and

display_df_with_delimiter(df,[0,1,2,5])

gives

enter image description here

Matus Dubrava :

This piece of code should add the desired lines to the table.

from IPython.display import display, HTML

CSS = """
.rendered_html td:nth-child(even) {
    border-left: 1px solid red;
}
"""

HTML('<style>{}</style>'.format(CSS))

Note that you can change the style of those linse by simply changing the definition of border-left attribute, i.e border-left: 2px solid green to make the lines thicker and green.

Here is a snapshot demonstrating the output.

enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=8538&siteId=1