简体   繁体   English

Pandas DataFrame:同时抑制科学计数法和操作边框样式?

[英]Pandas DataFrame: Suppress scientific notation and manipulate border style at the same time?

I am using the Python packages pyodbc and pandas. I have pulled some data from a SQL query into a dataframe and am attempting to get a tabular view of the dataframe with two characteristics:我正在使用 Python 包 pyodbc 和 pandas。我已将一些数据从 SQL 查询提取到 dataframe 中,并试图获得具有两个特征的 dataframe 的表格视图:

  1. Numeric columns are displayed with a specified decimal precision and no scientific notation.数字列以指定的小数精度显示,没有科学记数法。
  2. Column borders are thick and black.列边框很粗,呈黑色。

Can anyone help me to do this?谁能帮我做这个? I am working in Jupyter-Notebook, Python v3.6.5.我在 Jupyter-Notebook、Python v3.6.5 工作。 Here is my code so far.到目前为止,这是我的代码。 It gives me criterion #2 but not #1.它给了我标准#2 但不是#1。 If I run the last line which is currently commented out, I get criterion #1 but not #2.如果我运行当前被注释掉的最后一行,我得到标准#1 但不是#2。

import pyodbc as po
import pandas as pd
pd.set_option('display.float_format', '{:.5f}'.format)
conn=po.connect(DRIVER = '{SQL Server}',
                      SERVER = 'xxx',
                      DATABASE = 'yyy')
query_VD01="""select 1 union select 0.000001 union select 1000000"""

VD01=pd.read_sql(query_VD01,conn)
VD01.style.set_properties(**{'border-style':'solid'})
#display(VD01)

edit - I want 6 digits of decimal precision.编辑 - 我想要 6 位小数精度。 Here is the result of running VD01.head():下面是运行 VD01.head() 的结果:

0   0.00000
1   1.00000
2   1000000.00000

If I had set display.float_format to {:.6f} I instead get this from VD01.head():如果我将 display.float_format 设置为 {:.6f},我会从 VD01.head() 中获取:

0   0.000001
1   1.000000
2   1000000.000000

chain together the style and you get what you want.样式链接在一起,您就会得到想要的东西。

VD01 = pd.DataFrame([0.000001,1.000000,1000000.000000])
VD01.style.format("{:.6f}").set_properties(**{'border-style':'solid'})


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

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