python blaze postgresql 无法打印 "distinct" 鸢尾花种
python blaze postgresql can't print "distinct" iris species
通过 this tutorial about 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)
如何在 table 中实际打印不同的物种?
注意:我知道我在代码中的 "species" 部分使用小写字母 "s",否则我只会收到一条错误消息:'Field' object has no attribute 'Species'
这里的打印机制让你有点误会。
__str__
实现(Python 的 print
函数调用)returns 表达式的字符串版本。
__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
,这样做:
In [5]: odo(iris.species.distinct(), pd.Series)
Out[5]:
0 Iris-versicolor
1 Iris-virginica
2 Iris-setosa
Name: species, dtype: object
好的,我想我现在知道了。 YouTube 视频的其余部分使其更加清晰。
我应该做类似 output = odo(mySpecies, pdDataFrame)
或 output = odo(mySpecies, list)
的事情,然后 print output
来进行转换。
其他solutions/points 欢迎。
通过 this tutorial about 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)
如何在 table 中实际打印不同的物种?
注意:我知道我在代码中的 "species" 部分使用小写字母 "s",否则我只会收到一条错误消息:'Field' object has no attribute 'Species'
这里的打印机制让你有点误会。
__str__
实现(Python 的 print
函数调用)returns 表达式的字符串版本。
__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
,这样做:
In [5]: odo(iris.species.distinct(), pd.Series)
Out[5]:
0 Iris-versicolor
1 Iris-virginica
2 Iris-setosa
Name: species, dtype: object
好的,我想我现在知道了。 YouTube 视频的其余部分使其更加清晰。
我应该做类似 output = odo(mySpecies, pdDataFrame)
或 output = odo(mySpecies, list)
的事情,然后 print output
来进行转换。
其他solutions/points 欢迎。