简体   繁体   中英

Using python / Jupyter Notebook, how to prevent row numbers from printing?

When I print a database, it prints with row numbers. In spyder, I use print(df), while in jupyter notebook, I am using df to print. Any suggestions?

        brand         mean_price  mean_mileage(km)  total_listed 
 0      volkswagen    3913        140769            8046
 1      opel          2697        138046            4065
 2      bmw           5313        143804            4048
 3      mercedes_benz 4790        143507            3526
 4      audi          5221        144614            3031
 5      ford          2912        136321            2483

这应该有效

print (df.to_string(index=False))

If you want to keep something similar to dataframe formatting as you expressed in a comment to @Prachiti, would be assign another column to be the index. For example, you could make brand the index with the following:

df = df.set_index('brand')

Alternatively, display it as HTML.
The simplest aproach is to remove the index column when doing the conversion to HTML, as shown here . It will display like a dataframe.

from IPython.display import HTML
HTML(df.to_html(index=False))

However, it is even possible just to combine with CSS to display the HTML table in similar style as dataframe rendering while using display:none; to hide the first column.

The code in total would be as shown below; however, you could break it into as many cells as desired:

html=df.to_html()
# css modified from cell #9  https://nbviewer.jupyter.org/github/fomightez/dataframe2img/blob/master/index.ipynb
# and https://stackoverflow.com/a/37811124/8508004
css = """
<style type=\"text/css\">
table {
color: #333;
font-family: Helvetica, Arial, sans-serif;
width: 340px;
border-collapse:
collapse; 
border-spacing: 0;
}
td, th {
border: 1px solid transparent; /* No more visible border */
font-size: 12.8px;
height: 24px;
}
th {
background: #FAFAFA; /* Darken header a bit */
font-weight: bold;
}
td {
background: #FAFAFA;
text-align: center;
font-size: 11.8px;
}
table tr:nth-child(odd) td{
background-color: white;
}
th:nth-child(1) { display:none; }
</style>
"""
from IPython.display import HTML
HTML(css+html)

Here is an image showing the result with an example dataframe (code in first cell here ): 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM