Skip to content

Commit bcf6bad

Browse files
committed
Refactor code structure for improved readability and maintainability
1 parent 0eea77c commit bcf6bad

13 files changed

Lines changed: 2935 additions & 2 deletions

.vscode/settings.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
22
"python-envs.defaultEnvManager": "ms-python.python:system",
3-
"python-envs.defaultPackageManager": "ms-python.python:pip",
4-
"python-envs.pythonProjects": []
3+
"python-envs.defaultPackageManager": "ms-python.python:pip"
54
}

dacon/1.preprocessing.ipynb

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,117 @@
102102
"cell_type": "markdown",
103103
"id": "90af824f",
104104
"metadata": {},
105+
"source": [
106+
"## function 설명\n",
107+
"\n",
108+
"```python\n",
109+
"train['key'].value_counts() # value_counts() - 판다스 Series 객체에 고유값의 빈도를 계산하여 반환 \n",
110+
"```\n",
111+
"\n",
112+
"## raw string \n",
113+
"\n",
114+
"- 문자열 앞에 `r`을 표시, 문자열 내에 특수 시퀀스가 특별한 처리 없이 문자 그대로 취급 되도록함. \n",
115+
"\n",
116+
"```python\n",
117+
"pattern = r'안전'\n",
118+
"train['법규위반_안전'] = train['법규위반'].str.contains(pattern)\n",
119+
"\n",
120+
"display(train.head(3))\n",
121+
"\n",
122+
"```"
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"id": "0c40d008",
128+
"metadata": {},
129+
"source": [
130+
"## 문제\n",
131+
"\n",
132+
"```\n",
133+
"```"
134+
]
135+
},
136+
{
137+
"cell_type": "markdown",
138+
"id": "70c40ebb",
139+
"metadata": {},
140+
"source": [
141+
"str.replace 함수를 사용하여 도로형태 칼럼의 값에서 공백(\\s), 하이픈(-), 공백(\\s)을 포함한 이후의 모든 문자(.*)를 모두 제거('')하여, 각 값의 첫 번째 단어만 남겨보세요."
142+
]
143+
},
144+
{
145+
"cell_type": "markdown",
146+
"id": "4387a0ca",
147+
"metadata": {},
148+
"source": [
149+
"```python\n",
150+
"# '도로형태' 열에서 ' - ' 이후 문자열 제거\n",
151+
"pattern = r'\\s-\\s.*'\n",
152+
"train['도로형태_대분류'] = train['도로형태'].str.replace(pattern, '', regex=True)\n",
153+
"\n",
154+
"display(train.head(3))\n",
155+
"```"
156+
]
157+
},
158+
{
159+
"cell_type": "markdown",
160+
"id": "68fde62e",
161+
"metadata": {},
162+
"source": [
163+
"# 캡처 그룹"
164+
]
165+
},
166+
{
167+
"cell_type": "markdown",
168+
"id": "3b04c085",
169+
"metadata": {},
170+
"source": [
171+
"# 문제\n",
172+
"\n",
173+
"```\n",
174+
"str.extract 메서드를 사용하여 시군구 칼럼에 '남구' 혹은 '달서구'가 존재할 경우 해당 값을 추출한 후 시군구_특정 열에 저장해보세요.\n",
175+
"시군구 열에 해당 값이 존재하지 않을 경우 시군구_특정 칼럼의 값을 '기타'로 저장합니다.\n",
176+
"```\n",
177+
"\n",
178+
"## 정답\n",
179+
"\n",
180+
"```python\n",
181+
"pattern = r'(남구|달서구)'\n",
182+
"\n",
183+
"# '남구' 또는 '달서구'를 추출하고, 해당되지 않는 경우 '기타'로 표시\n",
184+
"train['시군구_특정'] = train['시군구'].str.extract(pattern)\n",
185+
"train['시군구_특정'] = train['시군구_특정'].fillna('기타')\n",
186+
"\n",
187+
"display(train.head(3))\n",
188+
"```"
189+
]
190+
},
191+
{
192+
"cell_type": "markdown",
193+
"id": "e169b702",
194+
"metadata": {},
195+
"source": [
196+
"# 문제\n",
197+
"\n",
198+
"```\n",
199+
"그룹, 숫자를 의미하는 특수시퀀스(\\d), 반복자를 사용하여 패턴을 정의하여 사고일시 칼럼에서 연도, 월, 일, 시간 값을 추출하여 연, 월, 일, 시간 칼럼에 저장해보세요.\n",
200+
"```\n",
201+
"\n",
202+
"## 답\n",
203+
"\n",
204+
"```python\n",
205+
"time_pattern = r'(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2})'\n",
206+
"\n",
207+
"train[['연', '월', '일', '시간']] = train['사고일시'].str.extract(time_pattern)\n",
208+
"display(train.head(3))\n",
209+
"```"
210+
]
211+
},
212+
{
213+
"cell_type": "markdown",
214+
"id": "ff0fc7e7",
215+
"metadata": {},
105216
"source": []
106217
}
107218
],

0 commit comments

Comments
 (0)