반응형

/*******************************************************************************************************************
-- Title : [MSPy] sp_execute_external_script를 활용한 Python Script 실행 예제-2
-- Reference : www.sqlshack.com
-- Key word : sp_execute_external_script .csv .json csv json data_load bulk insert json.load
*******************************************************************************************************************/

■ Scripts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
 
use tempdb;
go
 
-------------------------------------------------
-- Creating table
-------------------------------------------------
DROP TABLE IF EXISTS dbo.[tbl_loanPrediction]
GO
 
CREATE TABLE [dbo].[tbl_loanPrediction] (
Loan_ID varchar(50),
gender varchar(50),
Married varchar(50),
Dependents varchar(50),
Education varchar(50),
SelfEmployed varchar(50),
ApplicationIncome varchar(50),
CoapplicantIncome varchar(50),
LoanAmount float ,
Loan_Amount_Term varchar(50),
Credit_History  varchar(50),
Property_Area varchar(50)
);
 
-------------------------------------------------
-- loading csv into table
-------------------------------------------------
BULK
INSERT [tbl_LoanPrediction]
FROM 'D:\sample.csv'
WITH
(
FIRSTROW = 2,    -- field명 때문에 2행부터 적재
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
 
 
-------------------------------------------------
-- Checking data
-------------------------------------------------
SELECT *
FROM [tbl_LoanPrediction]
GO
 
 
-------------------------------------------------
-- Querying csv using pandas
-------------------------------------------------
EXEC sp_execute_external_script 
@language = N'Python',
@script = N'
import pandas as pd
import numpy as np
df = pd.read_csv("D:\\sample.csv", index_col="Loan_ID")
print(df)';
 
 
-------------------------------------------------
-- Extraction data using logical conditiion
-------------------------------------------------
exec sp_execute_external_script 
@language = N'Python',
@script = N'
import pandas as pd
import numpy as np
df = pd.read_csv("D:\\sample.csv", index_col="Loan_ID")
print(df[["Gender","Education"]])
print("-"*30)
print(df.loc[(df["Gender"]=="Female") & (df["Education"]=="Not Graduate"), ["Gender","Education"]])
';
 
 
-------------------------------------------------
-- Data loading into the Temptable using pandas
-------------------------------------------------
DROP TABLE IF EXISTS dbo.#Loan
GO
 
SELECT TOP 5 *  FROM tbl_LoanPrediction;
 
CREATE TABLE #Loan
( Loan_ID varchar(20)
, Gender varchar(10null
, Education varchar(30)null
, MaritalStatus char(3)
);
 
INSERT INTO #Loan
exec sp_execute_external_script 
@language = N'Python',
@script = N'
import pandas as pd
import numpy as np
df = InputDataSet
out_df = df.loc[(df["gender"]=="Female") & (df["Education"]=="Not Graduate") & (df["Married"]=="No"), 
["Loan_ID","gender","Education","Married"]]',
@input_data_1 = N'SELECT * FROM [tbl_LoanPrediction]',
@output_data_1_name = N'out_df';
 
select * from #Loan;
 
 
-------------------------------------------------
-- Pivoting
-------------------------------------------------
EXEC sp_execute_external_script @language =N'Python',
@script=N'
import pandas as pd
import numpy as np
df = pd.read_csv("D:\\sample.csv", index_col="Loan_ID")
#-- Determine pivot table
Out_df = df.pivot_table(values=["LoanAmount"],index=["Gender","Married","Self_Employed"], aggfunc=np.mean)
print(Out_df)',
@output_data_1_name = N'Out_df';
 
 
-------------------------------------------------
-- Pivoting-2
-------------------------------------------------
DROP TABLE IF EXISTS dbo.#Movies
GO
 
CREATE TABLE #Movies
(MovieType VARCHAR(20), SalesYear INT, MovieSales int);
GO
 
INSERT INTO #Movies VALUES('Comedy'201411201);
INSERT INTO #Movies VALUES('History'201412939);
INSERT INTO #Movies VALUES('Comedy'201310436);
INSERT INTO #Movies VALUES('Comedy'20139346);
INSERT INTO #Movies VALUES('Comedy'20147214);
INSERT INTO #Movies VALUES('Comedy'20145800);
INSERT INTO #Movies VALUES('Comedy'20138922);
INSERT INTO #Movies VALUES('History'20137462);
 
SELECT * FROM #Movies;
 
SELECT *
FROM #Movies
  PIVOT(SUM(MovieSales) 
  FOR SalesYear IN([2013], [2014])
  ) AS PivotSales;
 
exec sp_execute_external_script 
@language = N'Python',
@script=N'
import pandas as pd
import numpy as np
in_df = InputDataSet
#Determine pivot table
out_df = in_df.pivot_table(values=["MovieSales"], index=["MovieType","SalesYear"], aggfunc=np.sum)
print(out_df)
',
@input_data_1 = N'SELECT * FROM #Movies',
@output_data_1_name = N'out_df';
 
 
-------------------------------------------------
-- Extraction data from JSON
-------------------------------------------------
EXEC sp_execute_external_script @language =N'Python',
@script=N'
import os
import json
script_dir = os.path.dirname("d:\\")
file_path = os.path.join(script_dir, "sample.json")
print(file_path)
print("Name" + "----- "+"Size" + "---- " + "%Free")
with open(file_path) as json_file:  
    data = json.load(json_file)
    for r in data["diskspace"]:
        if (float(r["freespace"]) > 5.00):
            print(r["server"] + "-- "+ r["size"] +"-- " +r["percentage"])
';
cs

 
■ Files

sample.csv

sample.json


 
 
 

반응형

+ Recent posts