Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Jupyter Notebook print dataframe borders

I'm probably have a simple question but I can't seem to find a solution online. If I create a dataframe "df" in a Jupyter notebook and then I print it out using print(), the table displayed in my broswer does not show any borders at all. For example:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(5,2), columns=["a","b"])
print(df)

This will show a table with no border. I read Jupyter pandas.DataFrame output table format configuration but that seems to help if I simply type

df.head()

not the output of the print() function. Does somebody have any suggestions? Many thanks

like image 387
Angelo Avatar asked Apr 17 '18 22:04

Angelo


People also ask

How do I print a Dataframe neatly?

You can use the print() method to print the dataframe in a table format. You can convert the dataframe to String using the to_string() method and pass it to the print method which will print the dataframe.

How do I print multiple lines in a Jupyter notebook?

Luckily, there's a Jupyter setting that you can change to print multiple outputs. The ast_node_interactivity setting allows you to choose which results are shown as outputs. By setting it to 'all', every assign and expression will be shown.

How do you display Dataframe next to plot in Jupyter notebook?

Set the figure size and adjust the padding between and around the subplots. Make a Pandas dataframe with straight and square keys. Create a new figure or activate an existing figure using figure() method.

How do I print a dataset in Jupyter notebook?

Print the data In a Jupyter notebook, simply typing the name of a data frame will result in a neatly formatted outputs. This is an excellent way to preview data, however notes that, by default, only 100 rows will print, and 20 columns.


3 Answers

@Angel, you can add this script in any cell to introduce table borders. Use df instead of print(df)

%%HTML
<style type="text/css">
table.dataframe td, table.dataframe th {
    border: 1px  black solid !important;
  color: black !important;
}
</style>

enter image description here

like image 130
MEdwin Avatar answered Oct 20 '22 22:10

MEdwin


Instead of print(df) you use:

from IPython.display import display
display(df)
like image 6
Abhishek Kumar Gupta Avatar answered Oct 20 '22 21:10

Abhishek Kumar Gupta


The answer of @MEdwin is perfect, just need to close the style tag, otherwise the broken HTML results in 'eated up' output of several output cells, which follows the HTML code.

%%HTML
<style type="text/css">
table.dataframe td, table.dataframe th {
    border: 1px  black solid !important;
  color: black !important;
}
</style>
like image 3
Anton Golubev Avatar answered Oct 20 '22 20:10

Anton Golubev