프로그래밍 공부
파이썬 1일차
3452
2025. 5. 7. 17:48
파이썬 변수선언
변수 = 값
출력
변수 or print(변수) or print(값)
라이브러리
넘파이
array
import numpy as np;
a = np.array([1,2,3,4,5]);
b = np.array(['대한민국', '포르투갈', '가나', '우루과이']);
c = np.array([1,2,'대한민국','포르투갈']);
print(a);
print(b);
print(c);
[1 2 3 4 5] ['대한민국' '포르투갈' '가나' '우루과이'] ['1' '2' '대한민국' '포르투갈']
판다스
Series
import pandas as pd;
s = pd.Series(['대한민국', '포르투갈', '가나', '우루과이'], index = ['가', '나', '다', '라'], name = "2022 카타르월드컵 H조");
print(s);
가 대한민국 나 포르투갈 다 가나 라 우루과이 Name: 2022 카타르월드컵 H조, dtype: object
Dictionary형
a1 = pd.DataFrame({"a" : [1,2,3], "b" : [4,5,6], "c" : [7,8,9]}, index = ['가', '나', '다'])
a1
List형
a2 = pd.DataFrame([[1,2,3], [4,5,6], [7,8,9]],['a','b','c'])
a2
데이터 불러오기
csv 파일 불러오기 read_csv
import pandas as pd;
import numpy as np;
flight = pd.read_csv('./Clean_Dataset.csv', encoding = "cp949");
flight
데이터 저장하기
csv 파일로 저장하기 to_csv
a2.to_csv('./result_a2.csv')
원하는 칼럼만 가지고 데이터프레임 만들기
flight2 = pd.read_csv('./Clean_Dataset.csv', index_col='stops', usecols=['stops', 'departure_time', 'arrival_time', 'destination_city']);
flight2
크로스탭
pd.crosstab(index=flight.source_city, columns=flight.arrival_time)
처음 n개 자료 가져오기
flight.head()
기본값 5개
마지막 n개 자료 가져오기
flight.tail()
기본값 5개
head와 tail 괄호 안에 숫자를 넣으면 해당 숫자만큼의 자료를 가져온다.
데이터프레임 행과 열 갯수 세기
flight.shape
(300153, 12)
컬럼 확인하기
flight.columns
Index(['Unnamed: 0', 'airline', 'flight', 'source_city', 'departure_time',
'stops', 'arrival_time', 'destination_city', 'class', 'duration',
'days_left', 'price'],
dtype='object')
데이터 유형, 컬럼의 데이터 수 확인하기
flight.info()
컬럼별 요약 통계량
flight.describe()
컬럼의 데이터타입
flight.dtypes
지정된 컬럼의 값에 대한 발생 횟수
flight['source_city'].value_counts()