是否可以在 greenplum 数据库中使用 pandas,如果可以,如何使用? 我在 greenplum 里面。我正在创建一个函数:
CREATE OR REPLACE FUNCTION myfunction() RETURNS
text AS $$
...
python code
...
rv = plpy.execute("SELECT * FROM mytable")
...
$$ LANGUAGE plpythonu;
SELECT public.myfunction()
命令 rv = plpy.execute("SELECT * FROM mytable")
生成一个 PlyResult 类型的对象。在这一点上,我想用 python pandas 来分析 rv 中的数据,例如。如何将 rv 转换为数据帧?
谢谢!
最佳答案
这是我的例子之一:
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;
我的博客中还有更多Greenplum-Pandas-Numpy-等集成的例子: https://dwhsys.com/2018/05/06/data-mining-in-mpp-database/
关于python - Plpython 程序中的 Greenplum pandas 数据框集成(来自数据库内部),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51305432/