繁体   English   中英

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

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

当我打印数据库时,它会打印行号。 在 spyder 中,我使用 print(df),而在 jupyter notebook 中,我使用 df 进行打印。 有什么建议?

        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))

如果您想保留类似于您在对@Prachiti 的评论中所表达的数据帧格式的内容,将分配另一列作为索引。 例如,您可以使用以下内容为index打上烙印:

df = df.set_index('brand')

或者,将其显示为 HTML。
最简单的形式给出了是在做转换为HTML时删除索引列,如图所示这里 它将像数据框一样显示。

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

但是,甚至可以仅与 CSS 结合使用display:none;以与数据帧渲染类似的样式显示 HTML 表格display:none; 隐藏第一列。

总代码如下所示; 但是,您可以根据需要将其分解为多个单元格:

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)

这是显示带有示例数据帧的结果的图像( 此处第一个单元格中的代码): 在此处输入图片说明

暂无
暂无

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

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