小编Stu*_*ent的帖子

如何在Python的PyPyODBC模块中使用FetchAll?

我正在尝试使用PyPyODBC,特别是对这个例子的看法.但是,我对创建一个新表并不感兴趣,我只是想读取数据(它驻留在SQL Server中的数据库/表中)并在Python中使用它.

下面的代码工作正常,但以下部分( for row in cur.fetchall():)除外.

from __future__ import print_function
import pypyodbc
from  pandas import *
import pandas as pd
con = pypyodbc.connect(driver = '{SQL Server}', server = 'WIN-SAV35R5AKCQ',database ='V2.0.6_X12_837I')

cur = con.cursor()
cur.execute('''SELECT * FROM TS837_2400;''')
cur.commit()

for d in cur.description: 
    print (d[0], end=" ")

print('')

for row in cur.fetchall():
    for field in row: 
        print (field, end=" ")
    print ('')

cur.close()
con.close()
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误.

---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
<ipython-input-70-c08d5a03056a> in <module>() …
Run Code Online (Sandbox Code Playgroud)

python sql-server ipython pypyodbc

2
推荐指数
1
解决办法
7573
查看次数

如何在Pandas中为字符串添加前导零格式?

目标:['Birth Month']使用前导零进行格式化

目前,我有这个代码:

import pandas as pd
import numpy as np

df1=pd.DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6])])
df1['Birth Year']= np.random.randint(1905,1995, len(df1))
df1['Birth Month']= str(np.random.randint(1,12, len(df1))).zfill(2)
df1
Run Code Online (Sandbox Code Playgroud)

这会生成一个['Birth Month']不是我需要的值列表:

    A   B   Birth Year  Birth Month
0   1   4   1912        [4 5 9]
1   2   5   1989        [4 5 9]
2   3   6   1921        [4 5 9]
Run Code Online (Sandbox Code Playgroud)

相反,我正在寻找以下值中的值和格式['Birth Month']:

    A   B   Birth Year  Birth Month
0   1   4   1912        04
1   2   5   1989 …
Run Code Online (Sandbox Code Playgroud)

python string numpy dataframe pandas

2
推荐指数
1
解决办法
4374
查看次数

在 HTML 表格中,如何使用 python 在 jupyter 笔记本中的绘图旁边添加文本?

关于如何创建 1 X 2 HTML 表,其中单元格 {0} 是 matplotlib 图,单元格 {1} 是 Python 3.X 的文本描述,有什么想法吗?

import matplotlib.pyplot as plt
from io import BytesIO
%matplotlib inline

def add_split_screen(fig, text, iwidth=None):

    figdata = BytesIO()
    fig.savefig(figdata, format='png') 
    figdata.seek(0)
    figdata
    figdata.close()
    iwidth = ' width={0} '.format(iwidth) if iwidth is not None else ''
    datatable = '<table><tr><td>{0}</td><td>{1}</td></tr></table>'.format(figdata, text)
    display(HTML(datatable)) 
Run Code Online (Sandbox Code Playgroud)

设置测试用例:

fig, ax = plt.subplots(1,1, figsize=(6,4))
ax.plot([1,2,3])
text = '<h4>Special Chart:</h4><BR>Description of chart will go here.'
Run Code Online (Sandbox Code Playgroud)

然后在 Jupyter 笔记本中运行该函数:

add_split_screen(fig, text, iwidth='500px')
Run Code Online (Sandbox Code Playgroud)

我的输出如下: 在此输入图像描述

然而,我有兴趣在 Jupyter 笔记本中实际看到该情节。

html matplotlib bytesio python-3.x jupyter-notebook

2
推荐指数
1
解决办法
2193
查看次数

Scikit-学习不正确地在IPython中更新

我试图使用IPython shell中的以下命令将IPython中的scikit-learn从0.13更新到版本0.14:

!pip install scikit-learn --update
Run Code Online (Sandbox Code Playgroud)

一切似乎都在更新,但是当我查看点子日志时,我会看到以下内容(我将不胜感激):

 Rolling back uninstall of scikit-learn

  Replacing c:\anaconda\lib\site-packages\scikit_learn-0.13-py2.7.egg-info
Command C:\Anaconda\python.exe -c "import setuptools;__file__='c:\\users\\owner\\appdata\\local\\temp\\pip-build\\scikit-learn\\setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\owner\appdata\local\temp\pip-pqca_z-record\install-record.txt --single-version-externally-managed failed with error code 1 in c:\users\owner\appdata\local\temp\pip-build\scikit-learn

Exception information:
Traceback (most recent call last):
  File "C:\Anaconda\lib\site-packages\pip\basecommand.py", line 107, in main
    status = self.run(options, args)
  File "C:\Anaconda\lib\site-packages\pip\commands\install.py", line 261, in run
    requirement_set.install(install_options, global_options)
  File "C:\Anaconda\lib\site-packages\pip\req.py", line 1166, in install
    requirement.install(install_options, global_options)
  File "C:\Anaconda\lib\site-packages\pip\req.py", line 589, in install
    cwd=self.source_dir, filter_stdout=self._filter_install, show_stdout=False)
  File "C:\Anaconda\lib\site-packages\pip\util.py", line 612, in …
Run Code Online (Sandbox Code Playgroud)

pip rollback ipython python-2.7 scikit-learn

1
推荐指数
1
解决办法
1546
查看次数

在Bokeh python中创建雷达图的步骤是什么?

目标:在Bokeh python中创建雷达图

为了帮助您,这是我关注的图表类型: 在此处输入图片说明

我从Matplotlib 获得了这个图表示例,这可能有助于缩小解决方案上的差距,但是我看不到如何实现。

以下是我使用散景可以找到的最接近雷达图的示例:

from collections import OrderedDict
from math import log, sqrt

import numpy as np
import pandas as pd
from six.moves import cStringIO as StringIO

from bokeh.plotting import figure, show, output_file

antibiotics = """
bacteria,                        penicillin, streptomycin, neomycin, gram
Mycobacterium tuberculosis,      800,        5,            2,        negative
Salmonella schottmuelleri,       10,         0.8,          0.09,     negative
Proteus vulgaris,                3,          0.1,          0.1,      negative
Klebsiella pneumoniae,           850,        1.2,          1,        negative
Brucella abortus,                1,          2,            0.02,     negative
Pseudomonas aeruginosa,          850,        2,            0.4, …
Run Code Online (Sandbox Code Playgroud)

charts visualization matplotlib python-3.x bokeh

1
推荐指数
1
解决办法
2060
查看次数