반응형
/*
-- Title : [Py2.7] Pandas.DataFrame활용한 IP 데이터 분석 - dBRang
-- Key word : pandas dataframe 데이터 프레임 ipdata ip데이터 ip 데이터 엑셀 xlsx read_excel astype encode encode("utf-8") beautifulsoup concat merge join 조인 pivot pivot_table 피벗
*/
-- Python Script
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# -*- coding: utf-8 -*-
import os, sys
import pandas as pd
from bs4 import BeautifulSoup
import urllib2
import pymssql
import numpy as np
# ********************************************
# -- user_company 엑셀 호출
# df_user_company
# ********************************************
print("-" * 100 + "[1]") # ----- #
atcompanyfile = "c:\\samples\\wops\\user_company.xlsx"
df_user_company = pd.read_excel(atcompanyfile, sheetname="Sheet1")
df_user_company.head(5)
# ********************************************
# -- user_patent 엑셀 호출
# df_user_patent
# ********************************************
print("-" * 100 + "[2]") # ----- #
atpatent1file = "c:\\samples\\wops\\user_patent.xlsx"
df_user_patent = pd.read_excel(atpatent1file, sheetname="Sheet1")
df_user_patent.head(5)
# ********************************************
# -- company DF 통계 및 컬럼 타입 확인
# ********************************************
print("-" * 100 + "[3]") # ----- #
df_user_company.describe()
df_user_company.dtypes
# ********************************************
# -- user_patent에서 skey 기준으로 행정 정보 가져오기
# df_wops_admininfo
# ********************************************
print("-" * 100 + "[4]") # ----- #
df_wops_admininfo = pd.DataFrame(columns=("skey","curr_assn","curr_rept_assn","curr_rept_cd","expired_dt","lgl_stts"))
conn = pymssql.connect('servername', 'username', 'password', 'databasename')
cur = conn.cursor()
idx = 0
cnt_user_patent = len(df_user_patent)
for k in range(cnt_user_patent):
qry = 'exec up_ttt_admininfo @skey=' + str(df_user_patent["skey"][k])
cur.execute(qry)
rows = cur.fetchone()
while rows:
df_wops_admininfo.loc[idx] = [rows[0], rows[1], rows[2], rows[3], rows[4], rows[5]]
rows = cur.fetchone()
idx += 1
# -- 타입 변경 : float64 to int64
df_wops_admininfo["skey"] = df_wops_admininfo["skey"].astype(long)
df_wops_admininfo.head(5)
conn.close()
# ********************************************
# -- user_patent에서 skey 기준으로 패밀리 정보 가져오기
# df_wops_family
# ********************************************
print("-" * 100 + "[5]") # ----- #
df_wops_family = pd.DataFrame(columns=("skey","seq","fmly_skey","fkey","fmly_num","ctry_cnt","kr_cnt","us_cnt","jp_cnt"))
conn = pymssql.connect('servername', 'username', 'password', 'databasename')
cur = conn.cursor()
idx = 0
cnt_user_patent = len(df_user_patent)
for k in range(cnt_user_patent):
qry = 'exec up_ttt_family @skey=' + str(df_user_patent["skey"][k])
cur.execute(qry)
rows = cur.fetchone()
while rows:
df_wops_family.loc[idx] = [rows[0], rows[1], rows[2], rows[3], rows[4], rows[5], rows[6], rows[7], rows[8]]
rows = cur.fetchone()
idx += 1
# -- 타입 변경 : float64 to int64
df_wops_family["skey"] = df_wops_family["skey"].astype(long)
df_wops_family["fmly_skey"] = df_wops_family["fmly_skey"].astype(long)
df_wops_family.head(5)
conn.close()
# ********************************************
# -- user_patent에서 skey 기준으로 동일 IPC 특허 정보 가져오기
# ********************************************
print("-" * 100 + "[6]") # ----- #
df_wops_ipcpat = pd.DataFrame(columns=("skey","ipc","ctry_cd","doc_kind","appl_num","appl_dt","publ_num","publ_dt","grt_num","grt_dt"))
conn = pymssql.connect('servername', 'username', 'password', 'databasename')
cur = conn.cursor()
idx = 0
cnt_user_company = len(df_user_company)
for f in range(cnt_user_company):
qry = "exec up_ttt_ipcpat @ipc='" + str(df_user_company["ipc"][f]) + \
"'," + "@start_year='20150101', @end_year='20151231'"
cur.execute(qry)
rows = cur.fetchone()
while rows:
df_wops_ipcpat.loc[idx] = [rows[0], rows[1], rows[2], rows[3], rows[4], rows[5], rows[6], rows[7], rows[8], rows[9]]
rows = cur.fetchone()
idx += 1
# -- 타입 변경 : float64 to int64
df_wops_ipcpat["skey"] = df_wops_ipcpat["skey"].astype(long)
df_wops_ipcpat.head(5)
conn.close()
# ********************************************
# -- KIPRIS PLUS에서 기술 정보 획득위한 API 호출하여 DF에 저장
# df_kipris_techdeal
# ********************************************
print("-" * 100 + "[7]") # ----- #
kiprisURL1 = u"http://plus.kipris.or.kr/kipo-api/ipmarket/CommonTradePatentInfoService/getPatentPatentee?pem_user="
kiprisURL2 = u"&ServiceKey=Ffoec2gBOXnRp6nZC2ecN7r83RIn5=T2VSFfcBKpE7k="
df_kipris_techdeal = pd.DataFrame(columns=("com_nm", "no", "cr_dt", "deal_ti"))
idx = 0
for i in range(df_user_company.shape[0]):
com_nm = urllib2.quote(df_user_company["com_nm"][i].encode("utf-8"))
kiprisURL = kiprisURL1 + com_nm + kiprisURL2
kiprisPage = urllib2.urlopen(kiprisURL)
kiprisSoup = BeautifulSoup(kiprisPage, "html.parser")
rowcnt = len(kiprisSoup.find_all("item"))
if (rowcnt != 0):
for j in range(rowcnt):
df_kipris_techdeal.loc[idx] = [df_user_company["com_nm"][i],
j + 1,
kiprisSoup.find_all("createdate")[j].text,
kiprisSoup.find_all("seltitle")[j].text
]
idx += 1
df_kipris_techdeal.head(5)
# ********************************************
# -- user_patent와 wops_ipcpat 아래 Concatenate
# ********************************************
print("-" * 100 + "[8]") # ----- #
pd.concat([df_user_patent, df_wops_ipcpat])
# ********************************************
# -- user_patent와 wops_ipcpat 아래 Concatenate를 새로운 DF로 생성
# df_new_patlist
# ********************************************
print("-" * 100 + "[9]") # ----- #
df_new_patlist = pd.concat([df_user_patent, df_wops_ipcpat])
df_new_patlist.head(5)
# ********************************************
# -- new_patlist를 통해서 KSIC 정보 추출
# df_new_ksic
# ********************************************
print("-" * 100 + "[10]") # ----- #
# -- 기존 인덱스 초기화
df_new_patlist = df_new_patlist.reset_index(drop=True)
# -- KSIC 추출
df_new_ksic = pd.DataFrame(columns=("ipc", "pr_val", "ksic", "biz_nm"))
conn = pymssql.connect('servername', 'username', 'password', 'databasename')
cur = conn.cursor()
idx = 0
cnt_new_patlist = len(df_new_patlist)
for g in range(cnt_new_patlist):
qry = "exec up_ttt_ksic @ipc='" + str(df_new_patlist["ipc"][g]) + "';"
cur.execute(qry)
rows = cur.fetchone()
while rows:
df_new_ksic.loc[idx] = [rows[0], rows[1], rows[2], rows[3]]
rows = cur.fetchone()
idx += 1
df_new_ksic.head(5)
conn.close()
# ********************************************
# -- new_patlist와 new_ksic와의 Merge(inner)
# ********************************************
print("-" * 100 + "[11]") # ----- #
# -- new_patlist.ipc를 4자리로 절삭(안하고 아래 left_on 처럼 가능)
# df_new_patlist["ipc4"] = df_new_patlist["ipc"].str[:4]
pd.merge(df_new_patlist, df_new_ksic, how="inner", left_on=df_new_patlist["ipc"].str[:4], right_on="ipc")
# ********************************************
# -- new_patlist와 new_ksic와의 Merge(inner) 된 결과의 다운로드
# ********************************************
print("-" * 100 + "[12]") # ----- #
# -- DF 새로 생성
df_new_download = pd.merge(df_new_patlist, df_new_ksic, how="inner", left_on=df_new_patlist["ipc"].str[:4], right_on="ipc")
# -- 다운로드
excelOutPath = "C:\\samples\\wops\\new_patlist_ksic.xlsx"
writer = pd.ExcelWriter(excelOutPath, engine="xlsxwriter")
df_new_download.to_excel(writer, sheet_name="Sheet1", index=True)
writer.save()
# ********************************************
# -- user_patent와 wops_admininfo을 이용한 1:1 관계
# ********************************************
print("-" * 100 + "[12]") # ----- #
# -- 몇몇 ROW 삭제
df_user_patent = df_user_patent.drop(2, axis = 0)
df_user_patent = df_user_patent.drop(3, axis = 0)
df_user_patent = df_user_patent.drop(4, axis = 0)
df_wops_admininfo = df_wops_admininfo.drop(5, axis = 0)
df_wops_admininfo = df_wops_admininfo.drop(6, axis = 0)
df_wops_admininfo = df_wops_admininfo.drop(7, axis = 0)
# -- merge(inner)
pd.merge(df_user_patent, df_wops_admininfo, how="inner", left_index=True, right_index=True).head(5)
# - merge(outer)
pd.merge(df_user_patent, df_wops_admininfo, how="left", left_index=True, right_index=True).head(5)
pd.merge(df_user_patent, df_wops_admininfo, how="right", left_index=True, right_index=True).head(5)
# ********************************************
# -- df_wops_ipcpat 데이터셋 피벗팅
# ********************************************
print("-" * 100 + "[13]") # ----- #
pd.pivot_table(df_wops_ipcpat, index=["ctry_cd", "doc_kind"], values=["appl_dt","publ_dt"], columns=["ipc"], aggfunc=np.min)
|
-- Sample Files
반응형