Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add column delimiter to Pandas dataframe display

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

like image 567
user15964 Avatar asked Feb 07 '20 06:02

user15964


People also ask

How do I add a separator in pandas?

We use the python string format syntax '{:,. 0f}'. format to add the thousand comma separators to the numbers. Then we use python's map() function to iterate and apply the formatting to all the rows in the 'Median Sales Price' column.

How do I delimit a column in pandas?

Split column by delimiter into multiple columns Apply the pandas series str. split() function on the “Address” column and pass the delimiter (comma in this case) on which you want to split the column. Also, make sure to pass True to the expand parameter.

How do I display a column in a DataFrame?

You can use the loc and iloc functions to access columns in a Pandas DataFrame. Let's see how. If we wanted to access a certain column in our DataFrame, for example the Grades column, we could simply use the loc function and specify the name of the column in order to retrieve it.


3 Answers

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

like image 105
Matus Dubrava Avatar answered Oct 19 '22 09:10

Matus Dubrava


Try with pandas.style.apply(...):

from IPython.core.display import display, HTML

def css_border(x):
    return ["border-left: 1px solid red" if (i%2==0) else "border: 0px" for i, col in enumerate(x)]

html = (
    df.style.apply(css_border, axis=1).render()
)
display(HTML(html))
like image 23
Grzegorz Skibinski Avatar answered Oct 19 '22 10:10

Grzegorz Skibinski


Usually, we do this:

df[[0,1,2,3]]

and it will print columns 0 to 3 - if you want columns 0-1 and 2-3 printed separately:

from IPython.display import display, HTML

CSS = """
.output {
    flex-direction: row;
}
"""

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

and then call display for dataframe as:

display(df[[0,1]])
display(df[[2,3]])

Let me know if this helps!

UPDATE: Add lines in CSS as follows:

CSS = """
.rendered_html td:nth-last-child(2n) {
    border-right: 1px solid red;
}
"""

It adds red lines for every 2nd column - ofcourse you can edit the number of 'n' here as you like.

Hope this helps!

UPDATE: UPDATE: for every n-value CSS:

def delimit_views(delimiters):
    output = str()
    for delim in delimiters:
       output += ".rendered_html td:nth-child("+str(delim)+") {
       border-left: 1px solid red;}"
    return output

And now append this CSS to the HTML for your display.

Hope it works this time!

like image 3
Ramsha Siddiqui Avatar answered Oct 19 '22 09:10

Ramsha Siddiqui