6

Having the following code, how can I output the values from the external python script into a sql table that I can query after.

EXECUTE sp_execute_external_script 
@language = N'Python',
@script = N'
            import pandas as pd
            import numpy as np

            rd = np.random.randn(100)
            df = pd.DataFrame(rd).describe()
            print(df)'

Thanks in advance.

2
  • this post should be useful. It uses R, but the end goal remains the same. Commented Dec 23, 2017 at 17:15
  • Thank you, that was helpful. Commented Dec 23, 2017 at 17:53

2 Answers 2

7

Indeed, as the R version shows, consider INSERT INTO myTable ... using results from the Python executed script in TSQL, specifying @output_data. However, first create table to be appended to, aligning columns and types accordingly.

Additionally, since you use describe(), consider renaming columns prior to output. Finally, pandas is included by default for the Python Machine Learning Services in SQL Server 2016 as mentioned in this tutorial, so no need to import (possibly same with numpy).

DROP TABLE IF EXISTS myTable;

CREATE TABLE myTable (
    [aggregate] varchar(50) NOT NULL,
    [value] float NOT NULL
    )
GO

INSERT INTO myTable    
EXECUTE sp_execute_external_script 
@language = N'Python',
@script = N'import numpy as np

            rd = np.random.randn(100)
            df = pandas.DataFrame(rd).describe().reset_index().rename(columns={"index":"aggregate", 0:"value"})',
@output_data_1_name = N'df';

GO
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, this is exactly what I was looking for.
What if you were dealing with a dictionary?
@Hiram, not quite sure what you mean but convert dictionary to pandas data frame. For more, please ask a new question.
-2
declare @py nvarchar(max);

set @py = N'from pandas.io.json import json_normalize

rdd = {"documents": [{"id": "1", "score": 0.97},{"id": "2","score": 0.87},{"id": "3", "score": 0.78}],"errors": []}
print(type(rdd))

df = json_normalize(rdd, "documents")
print(df)
print(type(df))
'; 

drop table if exists apiresults
create table apiresults (id int, score float)

insert into apiresults
exec sp_execute_external_script 
    @language = N'Python',
    @script = @py,
    @output_data_1_name =N'df'

select * from apiresults

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.