简体   繁体   English

使用python / Jupyter Notebook,如何防止打印行号?

[英]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.在 spyder 中,我使用 print(df),而在 jupyter notebook 中,我使用 df 进行打印。 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.如果您想保留类似于您在对@Prachiti 的评论中所表达的数据帧格式的内容,将分配另一列作为索引。 For example, you could make brand the index with the following:例如,您可以使用以下内容为index打上烙印:

df = df.set_index('brand')

Alternatively, display it as HTML.或者,将其显示为 HTML。
The simplest aproach is to remove the index column when doing the conversion to HTML, as shown here .最简单的形式给出了是在做转换为HTML时删除索引列,如图所示这里 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;但是,甚至可以仅与 CSS 结合使用display:none;以与数据帧渲染类似的样式显示 HTML 表格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 ):这是显示带有示例数据帧的结果的图像( 此处第一个单元格中的代码): 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止Jupyter Notebook下载PDF超出边距打印? - How to prevent Jupyter Notebook download PDF from printing outside of margin? Python - 在 Jupyter Notebook 中绘制双重打印(使用 Statsmodel 时) - Python - Plot Double Printing in Jupyter Notebook (When Using Statsmodel) 如何停止在 jupyter notebook 中打印这个? - How to stop printing this in jupyter notebook? 如何在 Jupyter Notebook Python 3.6.2 中关闭行号 - How to Turn Off Line Numbers in Jupyter Notebook Python 3.6.2 Jupyter Notebook无法使用表格打印表格 - Jupyter Notebook not printing table using tabulate 防止在jupyter笔记本中输出matplotlib(python) - prevent matplotlib output in jupyter notebook (python) 在使用jupyter笔记本的python中,如何获取包含100多个列的1行,并使用关键字将其拆分为第二行? - In python using jupyter notebook, how do I take a 1 row that has 100 plus columns and split it into a second row using a key word? 在Jupyter Notebook中打印时如何在光标上存放信息 - How to repositon cursor while printing in Jupyter notebook 使用 jupyter notebook 在 python 中出现 FileNotFoundError - FileNotFoundError in python using jupyter notebook 使用elasticsearch的jupyter python笔记本 - jupyter python notebook using elasticsearch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM