안녕하세요, 최근에 하이퍼 파일에 대한 전처리를 다루고 있는데,
아무래도 파이썬을 통해 피벗이나 여러 가지 라이브러리를 활용할 수 있어
하이퍼 파일을 파이썬에서 여는 법에 대해 포스팅 하려고 합니다.
오늘의 실습 데이터는 태블로 국룰 실습 데이터인 슈퍼스토어 샘플 데이터를 활용해보겠습니다.
(KR)Superstore_Sample.hyper
0.94MB
1. tableauhyperapi 설치
pip install tableauhyperapi
먼저 "tableauhyperapi"를 설치해주도록 할게요
2. Hyper Process 시작
from tableauhyperapi import HyperProcess, Telemetry
# Hyper process 시작
hyper = HyperProcess(telemetry=Telemetry.SEND_USAGE_DATA_TO_TABLEAU)
print("Hyper process started")
3. Hyper 파일 경로 지정 및 파일에 연결
from tableauhyperapi import Connection
# Hyper 파일 경로 지정
hyper_file_path = "여기에 하이퍼 경로를 입력해주세요.hyper" # 경로와 파일명을 맞게 수정
# Hyper 파일에 연결
connection = Connection(endpoint=hyper.endpoint, database=hyper_file_path)
print(f"Connected to Hyper file: {hyper_file_path}")
4.데이터베이스의 모든 테이블 이름 가져오기
먼저 사용가능한 스키마를 확인하려고 한다면
# 사용 가능한 스키마 확인
schemas = connection.catalog.get_schema_names()
print(f"Available schemas: {schemas}")
이 코드를 통해 확인할 수 있습니다.
보통 Extract와 Public을 사용하는 것 같습니다.
# 데이터베이스의 모든 테이블 이름 가져오기
tables = connection.catalog.get_table_names(schema='Extract')
print(f"Tables in the database: {tables}")
이 코드를 통해 모든 테이블의 이름을 가져올 수 있습니다.
5. 첫번째 테이블 가져오기
# 첫 번째 테이블 가져오기 (테이블이 있을 경우)
if tables:
table = tables[0] # 첫 번째 테이블 선택
print(f"Processing table: {table}")
# 각 테이블의 내용을 SQL 쿼리로 가져오기
query = f"SELECT * FROM {table}"
rows = connection.execute_list_query(query)
print(f"Rows fetched from {table}: {rows[:5]}") # 상위 5개 행 출력
else:
print("No tables found in the database.")
6. 컬림 이름 가져오기
# 컬럼 이름 가져오기
if tables:
table_definition = connection.catalog.get_table_definition(table)
column_names = [column.name for column in table_definition.columns]
print(f"Columns in {table}: {column_names}")
7. 데이터 프레임으로 변환
import pandas as pd
# Pandas 데이터프레임으로 변환
if tables and rows:
df = pd.DataFrame(rows, columns=column_names)
df
Pandas Dataframe을 활용하여 파이썬에서 위와 같이 데이터를 조회할 수 있습니다.
8. 연결 및 Hyper Process 종료
# 연결 및 Hyper process 종료
connection.close()
hyper.close()
print("Hyper process and connection closed.")
마지막으로 해당 코드를 통해 프로세스를 종료할 수 있습니다.
위 기능을 활용하면, 태블로 자체적으로 전처리나 데이터를 다루는 것 보다
파이썬 라이브러리를 활용하여 더 다양한 데이터 핸들링이 가능할 것 같습니다.
도움이 되셨길 바라면서, 구독과 공감, 댓글 부탁 드립니다 ~
'Tableau > Tableau Desktop' 카테고리의 다른 글
Tableau 자체 머신러닝 기능 : 예측과 클러스터링 (0) | 2024.12.18 |
---|---|
주소 데이터 위경도 좌표로 변환하여 태블로로 시각화하기 (0) | 2024.10.03 |