728x90 ๐ป Programming83 [Airflow] Python ํจ์ ์คํํ๊ธฐ | PythonOperator ์ฌ์ฉ PythonOperator๋ Apache Airflow์์ Python ํจ์๋ฅผ ์คํํ๋ ์์ ์ ์ ์ํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์ฐ์ฐ์์ด๋ค. ์ด๋ฅผ ํตํด Python ํจ์๋ฅผ ํธ์ถํ์ฌ ๋ฐ์ดํฐ ์ฒ๋ฆฌ, ๊ณ์ฐ, ๋๋ ์ฌ์ฉ์ ์ง์ ์์ ์ ์ํํ ์ ์๋ค. ์๋๋ PythonOperator๋ฅผ ์ฌ์ฉํ๋ ๊ฐ๋จํ ์์ ์ด๋ค. from airflow import DAG from airflow.operators.python_operator import PythonOperator from datetime import datetime, timedelta # DAG ์ ์ dag = DAG( 'python_operator_example', description='Example DAG with PythonOperator', schedule_inte.. 2023. 11. 19. [Airflow] ์ ์คํฌ๋ฆฝํธ, ๋ช ๋ น์ด ์คํํ๊ธฐ | BashOperator ์ฌ์ฉ BashOperator๋ Apache Airflow์์ ์ ์คํฌ๋ฆฝํธ๋ ๋ช ๋ น์ด๋ฅผ ์คํํ๋ ์์ ์ ์ ์ํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์ฐ์ฐ์์ด๋ค. ์ด๋ฅผ ํตํด ์ธ๋ถ ํ๋ก๊ทธ๋จ, ์คํฌ๋ฆฝํธ ๋๋ ๋ช ๋ น์ด๋ฅผ ์คํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์๋ค. ๋ค์์ BashOperator๋ฅผ ์ฌ์ฉํ๋ ๊ฐ๋จํ ์์ ๋ก, ๊ฐ๋จํ Bash ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๊ณ ์ถ๋ ฅ์ ๋ก๊น ํ๋ค. from airflow import DAG from airflow.operators.bash_operator import BashOperator from datetime import datetime, timedelta # DAG ์ ์ dag = DAG( 'bash_operator_example', description='Example DAG with BashOperator', sched.. 2023. 11. 19. [Airflow] ์ผ์ ํ ๊ฐ๊ฒฉ์ผ๋ก DAG ์คํํ๊ธฐ (์ค์ผ์ค๋ง) | schedule_interval | cron ๊ธฐ๋ฐ ์ค์ผ์ค Apache Airflow์์ DAG์ ์ผ์ ํ ๊ฐ๊ฒฉ์ผ๋ก ์คํํ๋ ค๋ฉด schedule_interval ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ๋ค. ์ด ๋งค๊ฐ๋ณ์๋ DAG์ด ์คํ๋ ์ฃผ๊ธฐ๋ฅผ ๋ํ๋ด๊ณ , ์ฃผ๊ธฐ๋ timedelta ๊ฐ์ฒด๋ก ์ ์๋๋ค. ์๋ฅผ ๋ค์ด, ๋งค์ผ ์คํํ๋ ค๋ฉด timedelta(days=1)๊ณผ ๊ฐ์ด ์ ์ํ ์ ์๋ค. timedelta๋ฅผ ์ฌ์ฉํ๋ ๋ช ๊ฐ์ง ์์ ๋ฅผ ์ดํด๋ณด์. timedelta # ๋งค์ผ ์คํ schedule_interval=timedelta(days=1) # 3์ผ๋ง๋ค ์คํ schedule_interval=timedelta(days=3) # ๋งค์ฃผ ์์์ผ ์คํ schedule_interval=timedelta(weeks=1, days=1) # ๋งค์๊ฐ ์คํ schedule_interval=timedelta(hours=1.. 2023. 11. 19. [Airflow] Airflow & DAG ์ค๋ช Apache Airflow Apache Airflow๋ ๋ฐ์ดํฐ ํ์ดํ๋ผ์ธ์ ๊ด๋ฆฌํ๊ณ ์ค์ผ์ค๋งํ๊ธฐ ์ํ ์คํ ์์ค ํ๋ซํผ์ด๋ค. Airflow๋ ํ์ด์ฌ ์ฝ๋๋ฅผ ์ด์ฉํด ํ์ดํ๋ผ์ธ์ ๊ตฌํํ ์ ์๊ธฐ์ ํ์ด์ฌ ์ธ์ด๋ก ๊ตฌํํ ์ ์๋ ๋๋ถ๋ถ์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ์ปค์คํ ํ์ดํ๋ผ์ธ์ ๋ง๋ค ์ ์๋ค. ๋ํ ์ฝ๊ฒ ํ์ฅ ๊ฐ๋ฅํ๊ณ ๋ค์ํ ์์คํ ๊ณผ ํตํฉ์ด ๊ฐ๋ฅํ๋ค. ์๋ง์ ์ค์ผ์ค๋ง ๊ธฐ๋ฒ์ผ๋ก ํ์ดํ๋ผ์ธ์ ์ ๊ธฐ์ ์ผ๋ก ์คํํ๊ณ ์ ์ง์ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๊ณ ์คํ ์์ค๋ผ๋ ์ฅ์ ์ด ์๊ธฐ ๋๋ฌธ์ ๋ง์ ๊ธฐ์ ์์ Airflow๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค. (๋ฐ์ดํฐ ํ์ดํ๋ผ์ธ์ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์์ ์ ์กฐ์งํ๊ณ ์คํํ๊ธฐ ์ํ ์ผ๋ จ์ ๋จ๊ณ ๋ฐ ํ๋ก์ธ์ค) Apache Airflow์ ํน์ง ์ค์ผ์ค๋ง๊ณผ ๋ชจ๋ํฐ๋ง Airflow๋ ์์ ์ ์ค์ผ์ค๋งํ๊ณ ๊ฐ์ํ๋ ๋ฐ ์ฌ์ฉ๋จ ์.. 2023. 11. 19. [pandas] ํน์ ์ปฌ๋ผ์์ ํน์ ๋ฌธ์์ด์ด ํฌํจ๋ ํ ์ฐพ๊ธฐ | str.contains ํ๋ค์ค์์๋ ํน์ ์ปฌ๋ผ์์ ํน์ ๋ฌธ์์ด์ด ํฌํจ๋ ํ์ ์ฐพ์ ์ ์์ต๋๋ค. ์ด๋ฅผ ์ํด์๋ ํด๋น ์ปฌ๋ผ์ ๋ฌธ์์ด์ ๋ํด str.contains() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. str.contains() ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ contains_apple = df[df['์ปฌ๋ผ A'].str.contains('๊ฐ๋๋ค๋ผ')] "์ปฌ๋ผ A"์์ "๊ฐ๋๋ค๋ผ"๊ฐ ํฌํจ๋ ํ์ ์ฐพ๊ธฐ ์ํด์๋ ์์ ๊ฐ์ด ์ฌ์ฉํ๋ฉด ๋๋ค. ์ ๊ท ํํ์ ์ฌ์ฉ contains_pattern = df[df['A'].str.contains('์ฌ๊ณผ|๋ฐ๋๋', regex=True)] regex=True ๋ก ์ค์ ํ์ฌ ์ ๊ท ํํ์์ ์ฌ์ฉํ ์ ์๋ค. ์๋ฅผ ๋ค์ด, ํน์ ํจํด์ด๋ ๋ฌธ์์ด์ ์ ๊ท ํํ์์ผ๋ก ์ง์ ํ์ฌ ๊ฒ์ํ ์ ์๋ค. ์ ์์๋ '์ฌ๊ณผ' ๋๋ '๋ฐ๋๋'๊ฐ ํฌํจ๋.. 2023. 11. 17. [pandas] ํน์ ์ปฌ๋ผ์ ๊ฐ์ด ๊ณต๋ฐฑ์ธ ํ์ ์ ์ธํ๋ ๋ฐฉ๋ฒ | dropna ํ๋ค์ค์ ๋ฐ์ดํฐํ๋ ์์์ ํน์ ์ปฌ๋ผ์ ๊ฐ์ด ๊ณต๋ฐฑ(๋๋ ๋น ๋ฌธ์์ด)์ธ ํ์ ์ ์ธํ๊ณ ์ถ์ ๋ ์ด๋ป๊ฒ ํ๋ฉด ๋๋์ง ์์๋ณด์. ํน์ ์ปฌ๋ผ์ ๊ณต๋ฐฑ์ธ ํ ์ ๊ฑฐ import pandas as pd # ์ํ ๋ฐ์ดํฐํ๋ ์ ์์ฑ data = {'A': [1, 2, 3, 4], 'B': ['apple', 'banana', '', 'orange']} df = pd.DataFrame(data) # 'B' ์ปฌ๋ผ์ ๊ฐ์ด ๊ณต๋ฐฑ์ธ ํ ์ ์ธ df_no_empty_values = df[df['B'].str.strip() != ''] # ๊ฒฐ๊ณผ ์ถ๋ ฅ print(df_no_empty_values) ์ ์ฝ๋์์ df['B'].str.strip() != '' ๋ถ๋ถ์ 'B' ์ปฌ๋ผ์ ๊ฐ ๊ฐ์ ๋ํด ์ข์ฐ์ ๊ณต๋ฐฑ์ ์ ๊ฑฐํ๊ณ ๋น ๋ฌธ์์ด๊ณผ ๋น๊ตํ์ฌ ๊ณต๋ฐฑ์ธ.. 2023. 11. 17. ์ด์ 1 2 3 4 5 6 7 8 ยทยทยท 14 ๋ค์ 728x90