python blaze postgresql无法打印“不同”的虹膜物种

2024-10-02 00:36:56 发布

您现在位置:Python中文网/ 问答频道 /正文

通过this tutorial关于blaze,但是在本地postgresql数据库中使用iris数据集。在

在使用db.iris.Species.distinct()时,我似乎没有得到相同的输出(参见Ipython笔记本的第16页)。在

我的连接字符串是postgresql://postgres:postgres@localhost:5432/blaze_test

我的简单Python代码是:

import blaze as bz
db = bz.Data('postgresql://postgres:postgres@localhost:5432/blaze_test')
mySpecies = db.iris_data.species.distinct()
print mySpecies

我在控制台(使用Spyder IDE)得到的只是distinct(_55.iris_data.species)

如何在表格中实际打印不同的物种?在

注意:我知道我在代码中使用小写的“s”来表示“species”部分,否则我只会得到一个错误提示:'Field' object has no attribute 'Species'


Tags: 代码testlocalhostirisdbdatapostgresqlpostgres
2条回答

打印装置在这里让你有点不舒服。在

__str__实现(这是Python的print函数调用的)返回表达式的字符串版本。在

__repr__实现(在解释器中执行一行时调用)触发计算,从而允许您查看计算结果。在

In [2]: iris = Data(odo(os.path.abspath('./blaze/examples/data/iris.csv'), 'postgresql://localhost::iris'))

In [3]: iris
Out[3]:
    sepal_length  sepal_width  petal_length  petal_width      species
0            5.1          3.5           1.4          0.2  Iris-setosa
1            4.9          3.0           1.4          0.2  Iris-setosa
2            4.7          3.2           1.3          0.2  Iris-setosa
3            4.6          3.1           1.5          0.2  Iris-setosa
4            5.0          3.6           1.4          0.2  Iris-setosa
5            5.4          3.9           1.7          0.4  Iris-setosa
6            4.6          3.4           1.4          0.3  Iris-setosa
7            5.0          3.4           1.5          0.2  Iris-setosa
8            4.4          2.9           1.4          0.2  Iris-setosa
9            4.9          3.1           1.5          0.1  Iris-setosa
...

In [4]: iris.species.distinct()
Out[4]:
           species
0  Iris-versicolor
1   Iris-virginica
2      Iris-setosa

In [8]: print(str(iris.species.distinct()))
distinct(_1.species)

In [9]: print(repr(iris.species.distinct()))
           species
0  Iris-versicolor
1   Iris-virginica
2      Iris-setosa

如果要将结果推送到具体的数据结构中,如pandas.Series,请执行以下操作:

^{pr2}$

好吧,我想我现在知道了。YouTube视频的其余部分让它更清晰了一些。

我应该做一些类似^{{cd1>}或^{{cd2>}之类的事情,然后^{{cd3>}来完成转换。

欢迎使用其他解决方案/要点。

相关问题 更多 >

    热门问题