관리 메뉴

보리차

01) 데이터 분석에 필요한 파이썬 문법 본문

공부/데이터 분석

01) 데이터 분석에 필요한 파이썬 문법

보리콩 2021. 8. 30. 23:09

문자열 함수

 

.startswith( )

word = "superman"
print(word.startswith('s')	# True

 

.split( )

numbers = "  1  2  3  "
print(numbers.split())
>>> ['1', '2', '3']

numbers = "  1  2  3  "
print(numbers.split(' '))
>>> [' ', '1', ' ', '2',' ', '3', ' ']

 

대표적 공백 문자

' '	빈칸(스페이스바)
'\t'	Tab(Tab 키)
'\n'	Newline(엔터 키)

 

.lower( ) 대소문자 변환

intro = "My name is Elice!"
print(intro.upper())
>>> "MY NAME IS ELICE!"
print(intro.lower())
>>> "my name is elice!"

 

.replace( )

intro = "제 이름은 Elice입니다."
print(intro.replace(' ', ''))
>>> "제이름은Elice입니다."

 

 

 

파일 다루기

 

file = open('data.txt')
content = file.read()
file.close()
with open('data.txt') as file:
    content = file.read()   
# file.close() - 필요 없음
with open('data.txt') as file:
	for line in file:
    	~~
# 쓰기(write) 모드로 파일을 연다.
with open('data.txt', 'w') as file:
	file.write('Hello')

 

 

 

데이터 구조 다루기

튜플: 파이썬의 고유한 데이터 타입

리스트와 유사하지만 각 원소의 값을 수정할 수 없고 원소의 개수도 바꿀 수 없다. => 수정이 불가능

 

 

 

그래프 다루기

matplotlib(Mathematical Plot Library)

파이썬에서 그래프를 그릴 수 있게 하는 라이브러리

꺾은선 그래프, 막대 그래프 등을 모두 지원

 

# matplotlib의 일부인 pyplot 라이브러리를 불러옵니다.
import matplotlib.pyplot as plt

# 엘리스에서 차트를 그릴 때 필요한 라이브러리를 불러옵니다.
from elice_utils import EliceUtils
elice_utils = EliceUtils()

# 월별 평균 기온을 선언합니다. 수정하지 마세요.
years = [2013, 2014, 2015, 2016, 2017]
temperatures = [5, 10, 15, 20, 17]

#막대 차트를 출력합니다.   
def draw_graph():
    # 막대 그래프의 막대 위치를 결정하는 pos를 선언합니다.
    pos = range(len(years))  # [0, 1, 2, 3, 4]
    
    # 높이가 온도인 막대 그래프를 그립니다.
    # 각 막대를 가운데 정렬합니다.
    plt.bar(pos, temperatures, align='center')
    
    # 각 막대에 해당되는 연도를 표기합니다.
    plt.xticks(pos, years)
    
    # 그래프를 엘리스 플랫폼 상에 표시합니다.
    plt.savefig('graph.png')
    elice_utils.send_image('graph.png')

print('막대 차트를 출력합니다.')
draw_graph()

 

 

 

[출처: 앨리스AI트랙 2기 aitrack.elice.io]

'공부 > 데이터 분석' 카테고리의 다른 글

06) Pandas 심화  (0) 2021.09.05
05) Pandas  (0) 2021.09.03
04) NumPy  (0) 2021.09.02
03) 복잡한 형태의 데이터: csv, lambda, map, filter  (0) 2021.08.31
02) 데이터형 변환 - 딕셔너리, JSON, 집합, matplotlib  (0) 2021.08.31