For example, define
df=pd.DataFrame(np.random.randint(0,10,(6,6)))
df
Which gives below display in Jupyter notebook
My question is that is it possible to add a column delimiter to the dataframe like
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
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.
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.
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.
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.
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))
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With