plpython过程中的数据帧集成(从数据库内部)

plpython过程中的数据帧集成(从数据库内部),python,postgresql,pandas,server-side,greenplum,Python,Postgresql,Pandas,Server Side,Greenplum,有可能在greenplum数据库中使用熊猫吗?如果有,如何使用? 我在青梅里面。我正在创建一个具有以下功能的函数: CREATE OR REPLACE FUNCTION myfunction() RETURNS text AS $$ ... python code ... rv = plpy.execute("SELECT * FROM mytable") ... $$ LANGUAGE plpythonu; SELECT public.myfunction() mytable中的命令rv

有可能在greenplum数据库中使用熊猫吗?如果有,如何使用? 我在青梅里面。我正在创建一个具有以下功能的函数:

CREATE OR REPLACE FUNCTION myfunction() RETURNS
text AS $$ 
...
python code
...
rv = plpy.execute("SELECT * FROM mytable")
...

$$ LANGUAGE plpythonu;
SELECT public.myfunction()
mytable中的命令rv=plpy.executeSELECT*生成PlyResult类型的对象。现在,我想用python分析rv中的数据。如何将rv转换为数据帧? 谢谢大家!

也许你可以试试pd.DataFramerv[0:]。 下面是一个研究生考试

postgres=# do $$
postgres$# import numpy as np
postgres$# import pandas as pd
postgres$# 
postgres$# iris = plpy.execute("SELECT * FROM iris LIMIT 3")
postgres$# plpy.notice(type(iris[0:]))
postgres$# iris = pd.DataFrame(iris[0:])
postgres$# 
postgres$# X = iris[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']].values
postgres$# plpy.notice(type(X))
postgres$# plpy.notice(X)
postgres$# 
postgres$# $$ language plpython3u;
NOTICE:  <class 'list'>
NOTICE:  <class 'numpy.ndarray'>
NOTICE:  [[Decimal('5.10') Decimal('3.50') Decimal('1.40') Decimal('0.20')]
 [Decimal('4.90') Decimal('3.00') Decimal('1.40') Decimal('0.20')]
 [Decimal('4.70') Decimal('3.20') Decimal('1.30') Decimal('0.20')]]
DO
postgres=# 
也许您可以尝试pd.DataFramerv[0:]。 下面是一个研究生考试

postgres=# do $$
postgres$# import numpy as np
postgres$# import pandas as pd
postgres$# 
postgres$# iris = plpy.execute("SELECT * FROM iris LIMIT 3")
postgres$# plpy.notice(type(iris[0:]))
postgres$# iris = pd.DataFrame(iris[0:])
postgres$# 
postgres$# X = iris[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']].values
postgres$# plpy.notice(type(X))
postgres$# plpy.notice(X)
postgres$# 
postgres$# $$ language plpython3u;
NOTICE:  <class 'list'>
NOTICE:  <class 'numpy.ndarray'>
NOTICE:  [[Decimal('5.10') Decimal('3.50') Decimal('1.40') Decimal('0.20')]
 [Decimal('4.90') Decimal('3.00') Decimal('1.40') Decimal('0.20')]
 [Decimal('4.70') Decimal('3.20') Decimal('1.30') Decimal('0.20')]]
DO
postgres=# 

以下是我的一个例子:

drop function if exists describe_yelp();
create or replace function describe_yelp(
OUT stats text,
OUT stars numeric,
OUT cool numeric,
OUT useful numeric,
OUT funny numeric,
OUT txt_length numeric)
returns setof record
as $$
import pandas as pd
import numpy as np
import nltk
from nltk.corpus import stopwords
yelp=pd.DataFrame.from_records(plpy.execute('select * from yelp'))[['stars','cool','useful','funny','text']]
yelp['txt_length'] = yelp['text'].apply(len)
return yelp.describe().to_records()      
$$
language plpythonu;
在我的博客中,有更多的绿梅-熊猫-Numpy-等整合的例子:
以下是我的一个例子:

drop function if exists describe_yelp();
create or replace function describe_yelp(
OUT stats text,
OUT stars numeric,
OUT cool numeric,
OUT useful numeric,
OUT funny numeric,
OUT txt_length numeric)
returns setof record
as $$
import pandas as pd
import numpy as np
import nltk
from nltk.corpus import stopwords
yelp=pd.DataFrame.from_records(plpy.execute('select * from yelp'))[['stars','cool','useful','funny','text']]
yelp['txt_length'] = yelp['text'].apply(len)
return yelp.describe().to_records()      
$$
language plpythonu;
在我的博客中,有更多的绿梅-熊猫-Numpy-等整合的例子: