Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- QGIS
- 진심
- pandas
- 완결 웹툰
- 웹툰
- 로맨스
- 제주도
- 완결
- 넷플릭스
- 커피
- 아주 작은 습관의 힘
- geopandas
- 빅데이터 분석기사 필기
- 네이버 완결 웹툰
- 애니메이션
- 가족
- 이기적 출판사
- 습관
- 산책
- 제임스 클리어
- python
- 서귀포
- 네이버 웹툰
- 네이버
- 영화
- 사랑
- 액션
- 만화 영화
- 빅데이터 분석기사
- 이범선
Archives
- Today
- Total
JuJuKwakKwak
Day10 - Code 본문
시험 대비¶
초보자를 위한 파이썬 300제¶
In [1]:
# 051 리스트 생성
# 2016년 11월 영화 예매 순위 기준 top3는 다음과 같습니다.
# 영화 제목을 movie_rank 이름의 리스트에 저장해보세요. (순위 정보는 저장하지 않습니다.)
movie_rank = ['닥터 스트레인지', '스플릿', '럭키']
print(movie_rank)
['닥터 스트레인지', '스플릿', '럭키']
In [2]:
# 052 리스트에 원소 추가
# 051의 movie_rank 리스트에 "배트맨"을 추가하라.
movie_rank.append('배트맨')
print(movie_rank)
['닥터 스트레인지', '스플릿', '럭키', '배트맨']
In [3]:
# 053
# movie_rank 리스트에는 아래와 같이 네 개의 영화 제목이 바인딩되어 있다.
# "슈퍼맨"을 "닥터 스트레인지"와 "스플릿" 사이에 추가하라.
movie_rank = ['닥터 스트레인지', '스플릿', '럭키', '배트맨']
movie_rank.insert(1, '슈퍼맨')
print(movie_rank)
['닥터 스트레인지', '슈퍼맨', '스플릿', '럭키', '배트맨']
In [4]:
# 054
# movie_rank 리스트에서 '럭키'를 삭제하라.
movie_rank.remove('럭키') # .pop() : 인덱스 기준 / .remove() : 값 기준
print(movie_rank)
['닥터 스트레인지', '슈퍼맨', '스플릿', '배트맨']
In [5]:
movie_rank = ['닥터 스트레인지', '슈퍼맨', '스플릿', '럭키', '배트맨']
del movie_rank[3]
print(movie_rank)
['닥터 스트레인지', '슈퍼맨', '스플릿', '배트맨']
In [6]:
# 055
# movie_rank 리스트에서 '스플릿' 과 '배트맨'을 를 삭제하라.
movie_rank.remove('스플릿')
movie_rank.remove('배트맨')
print(movie_rank)
['닥터 스트레인지', '슈퍼맨']
In [7]:
movie_rank = ['닥터 스트레인지', '슈퍼맨', '스플릿', '배트맨']
del movie_rank[2]
del movie_rank[2]
print(movie_rank)
['닥터 스트레인지', '슈퍼맨']
In [8]:
# 056
# lang1과 lang2 리스트가 있을 때 lang1과 lang2의 원소를 모두 갖고 있는 langs 리스트를 만들어라.
lang1 = ["C", "C++", "JAVA"]
lang2 = ["Python", "Go", "C#"]
langs = lang1 + lang2
print(langs)
['C', 'C++', 'JAVA', 'Python', 'Go', 'C#']
In [9]:
# 057
# 다음 리스트에서 최댓값과 최솟값을 출력하라. (힌트: min(), max() 함수 사용)
nums = [1, 2, 3, 4, 5, 6, 7]
print('max:', max(nums))
print('min:', min(nums))
max: 7 min: 1
In [10]:
# 058
# 다음 리스트의 합을 출력하라.
nums = [1, 2, 3, 4, 5]
print(sum(nums))
15
In [11]:
# 059
# 다음 리스트에 저장된 데이터의 개수를 화면에 구하하라.
cook = ["피자", "김밥", "만두", "양념치킨", "족발", "피자", "김치만두", "쫄면", "소시지", "라면", "팥빙수", "김치전"]
print(len(cook))
12
In [12]:
# 060
# 다음 리스트의 평균을 출력하라.
nums = [1, 2, 3, 4, 5]
mean = sum(nums) / len(nums)
print(mean)
3.0
In [13]:
# 061
# price 변수에는 날짜와 종가 정보가 저장돼 있다.
# 날짜 정보를 제외하고 가격 정보만을 출력하라. (힌트 : 슬라이싱)
price = ['20180728', 100, 130, 140, 150, 160, 170]
print(price[1:])
[100, 130, 140, 150, 160, 170]
In [14]:
# 062
# 슬라이싱을 사용해서 홀수만 출력하라.
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(nums[::2])
[1, 3, 5, 7, 9]
In [15]:
# 063
# 슬라이싱을 사용해서 짝수만 출력하라.
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(nums[1::2])
[2, 4, 6, 8, 10]
In [16]:
# 064
# 슬라이싱을 사용해서 리스트의 숫자를 역 방향으로 출력하라.
nums = [1, 2, 3, 4, 5]
print(nums[::-1])
[5, 4, 3, 2, 1]
In [17]:
# 065
# interest 리스트에는 아래의 데이터가 바인딩되어 있다.
# interest 리스트를 사용하여 아래와 같이 화면에 출력하라.
interest = ['삼성전자', 'LG전자', 'Naver']
print(interest[0], interest[2])
삼성전자 Naver
In [18]:
# 066 join 메서드
# interest 리스트에는 아래의 데이터가 바인딩되어 있다.
# interest 리스트를 사용하여 아래와 같이 화면에 출력하라.
interest = ['삼성전자', 'LG전자', 'Naver', 'SK하이닉스', '미래에셋대우']
print(" ".join(interest))
삼성전자 LG전자 Naver SK하이닉스 미래에셋대우
In [19]:
# 067 join 메서드
# interest 리스트에는 아래의 데이터가 바인딩되어 있다.
# interest 리스트를 사용하여 아래와 같이 화면에 출력하라.
interest = ['삼성전자', 'LG전자', 'Naver', 'SK하이닉스', '미래에셋대우']
print('/'.join(interest))
삼성전자/LG전자/Naver/SK하이닉스/미래에셋대우
In [20]:
# 068 join 메서드
# interest 리스트에는 아래의 데이터가 바인딩되어 있다.
# join() 메서드를 사용해서 interest 리스트를 아래와 같이 화면에 출력하라.
interest = ['삼성전자', 'LG전자', 'Naver', 'SK하이닉스', '미래에셋대우']
print('\n'.join(interest))
삼성전자 LG전자 Naver SK하이닉스 미래에셋대우
In [21]:
# 069 문자열 split 메서드
# 회사 이름이 슬래시 ('/')로 구분되어 하나의 문자열로 저장되어 있다.
# 이를 interest 이름의 리스트로 분리 저장하라.
string = "삼성전자/LG전자/Naver"
interest = string.split('/')
print(interest)
['삼성전자', 'LG전자', 'Naver']
In [22]:
# 070 리스트 정렬
# 리스트에 있는 값을 오름차순으로 정렬하세요.
data = [2, 4, 3, 1, 5, 10, 9]
data.sort()
print(data)
[1, 2, 3, 4, 5, 9, 10]
In [23]:
data2 = sorted(data)
print(data2)
[1, 2, 3, 4, 5, 9, 10]
In [24]:
# 071
# my_variable 이름의 비어있는 튜플을 만들라.
my_variable = {}
print(my_variable)
{}
In [25]:
# 072
# 2016년 11월 영화 예매 순위 기준 top3는 다음과 같다.
# 영화 제목을 movie_rank 이름의 튜플에 저장하라. (순위 정보는 저장하지 않는다.)
movie_rank = ('닥터 스트레인지', '스플릿', '럭키')
print(movie_rank)
('닥터 스트레인지', '스플릿', '럭키')
In [26]:
# 073
# 숫자 1 이 저장된 튜플을 생성하라.
print((1,))
(1,)
In [27]:
# 074
# 다음 코드를 실행해보고 오류가 발생하는 원인을 설명하라.
# t = (1, 2, 3)
# t[0] = 'a'
# 튜플은 원소의 값을 바꿀 수 없다.
In [28]:
# 075
# 아래와 같이 t에는 1, 2, 3, 4 데이터가 바인딩되어 있다.
# t가 바인딩하는 데이터 타입은 무엇인가?
t = 1, 2, 3, 4
print(type(t))
<class 'tuple'>
In [29]:
# 076
# 변수 t에는 아래와 같은 값이 저장되어 있다.
# 변수 t가 ('A', 'b', 'c') 튜플을 가리키도록 수정 하라.
t = ('a', 'b', 'c')
t = ('A', 'B', 'C')
print(t)
('A', 'B', 'C')
In [30]:
# 077
# 다음 튜플을 리스트로 변환하라.
interest = ('삼성전자', 'LG전자', 'SK Hynix')
interest = list(interest)
print(interest)
['삼성전자', 'LG전자', 'SK Hynix']
In [31]:
# 078
# 다음 리스트를 튜플로 변경하라.
interest = ['삼성전자', 'LG전자', 'SK Hynix']
interest = tuple(interest)
print(interest)
('삼성전자', 'LG전자', 'SK Hynix')
In [32]:
# 079 튜플 언팩킹
# 다음 코드의 실행 결과를 예상하라.
temp = ('apple', 'banana', 'cake')
a, b, c = temp
print(a, b, c)
apple banana cake
In [33]:
# 080 range 함수
# 1 부터 99까지의 정수 중 짝수만 저장된 튜플을 생성하라.
box = tuple(range(2, 100, 2))
print(box)
(2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98)
In [34]:
box = []
for i in range(1, 100):
if (i%2) == 0:
box.append(i)
print(tuple(box))
(2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98)
In [35]:
# 081 별 표현식
a, b, *c = (0, 1, 2, 3, 4, 5)
print(a)
print(b)
print(c, type(c))
0 1 [2, 3, 4, 5] <class 'list'>
In [36]:
# 다음과 같이 10개의 값이 저장된 scores 리스트가 있을 때,
# start expression을 사용하여 좌측 8개의 값을 valid_score 변수에 바인딩하여라.
scores = [8.8, 8.9, 8.7, 9.2, 9.3, 9.7, 9.9, 9.5, 7.8, 9.4]
*valid_score, _, _ = scores
print(valid_score, len(valid_score))
[8.8, 8.9, 8.7, 9.2, 9.3, 9.7, 9.9, 9.5] 8
In [37]:
# 082
# 다음과 같이 10개의 값이 저장된 scores 리스트가 있을 때,
# start expression을 사용하여 우측 8개의 값을 valid_score 변수에 바인딩하여라.
scores = [8.8, 8.9, 8.7, 9.2, 9.3, 9.7, 9.9, 9.5, 7.8, 9.4]
_, _, *valid_score = scores
print(valid_score, len(valid_score))
[8.7, 9.2, 9.3, 9.7, 9.9, 9.5, 7.8, 9.4] 8
In [38]:
# 083
# 다음과 같이 10개의 값이 저장된 scores 리스트가 있을 때,
# start expression을 사용하여 가운데 있는 8개의 값을 valid_score 변수에 바인딩하여라.
scores = [8.8, 8.9, 8.7, 9.2, 9.3, 9.7, 9.9, 9.5, 7.8, 9.4]
_, *valid_score, _ = scores
print(valid_score, len(valid_score))
[8.9, 8.7, 9.2, 9.3, 9.7, 9.9, 9.5, 7.8] 8
In [39]:
# 084 비어있는 딕셔너리
# temp 이름의 비어있는 딕셔너리를 만들라.
temp = {}
print(temp)
{}
In [40]:
# 085
# 다음 아이스크림 이름과 희망 가격을 딕셔너리로 구성하라.
icecream = {
'메로나' : 1000,
'폴라포' : 1200,
'빵빠레' : 1800
}
print(icecream)
{'메로나': 1000, '폴라포': 1200, '빵빠레': 1800}
In [41]:
# 086
# 085 번의 딕셔너리에 아래 아이스크림 가격정보를 추가하라.
temp = {
'죠스바' : 1200,
'월드콘' : 1500
}
icecream.update(temp)
print(icecream)
{'메로나': 1000, '폴라포': 1200, '빵빠레': 1800, '죠스바': 1200, '월드콘': 1500}
In [42]:
# 087
# 다음 딕셔너리를 사용하여 메로나 가격을 출력하라.
print('메로나 가격: {}'.format(icecream['메로나']))
메로나 가격: 1000
In [43]:
# 088
# 다음 딕셔너리에서 메로나의 가격을 1300으로 수정하라.
ice = {'메로나': 1000,
'폴로포': 1200,
'빵빠레': 1800,
'죠스바': 1200,
'월드콘': 1500}
ice['메로나'] = 1300
print(ice['메로나'])
1300
In [44]:
# 089
# 다음 딕셔너리에서 메로나를 삭제하라.
ice = {'메로나': 1000,
'폴로포': 1200,
'빵빠레': 1800,
'죠스바': 1200,
'월드콘': 1500}
del ice['메로나']
print(ice)
{'폴로포': 1200, '빵빠레': 1800, '죠스바': 1200, '월드콘': 1500}
In [45]:
# 090
# 다음 코드에서 에러가 발생한 원인을 설명하라.
# icecream = {'폴라포': 1200, '빵빠레': 1800, '월드콘': 1500, '메로나': 1000}
# icecream['누가바']
# '누가바'라는 key가 없기 때문이다.
In [46]:
# 091 딕셔너리 생성
# 아래의 표에서, 아이스크림 이름을 키값으로, (가격, 재고) 리스트를 딕셔너리의 값으로 저장하라.
# 딕셔너리의 이름은 inventory로 한다.
inventory = {
'메로나' : [300, 20],
'비비빅' : [400, 3],
'죠스바' : [250, 100]
}
print(inventory)
{'메로나': [300, 20], '비비빅': [400, 3], '죠스바': [250, 100]}
In [47]:
# 092 딕셔너리 인덱싱
# inventory 딕셔너리에서 메로나의 가격을 화면에 출력하라.
inventory = {"메로나": [300, 20],
"비비빅": [400, 3],
"죠스바": [250, 100]}
print('{} 원'.format(inventory['메로나'][0]))
300 원
In [48]:
# 093 딕셔너리 인덱싱
# inventory 딕셔너리에서 메로나의 재고를 화면에 출력하라.
inventory = {"메로나": [300, 20],
"비비빅": [400, 3],
"죠스바": [250, 100]}
print('{} 개'.format(inventory['메로나'][1]))
20 개
In [49]:
# 094 딕셔너리 추가
# inventory 딕셔너리에 아래 데이터를 추가하라.
inventory = {"메로나": [300, 20],
"비비빅": [400, 3],
"죠스바": [250, 100]}
inventory['월드콘'] = [500, 7]
print(inventory)
{'메로나': [300, 20], '비비빅': [400, 3], '죠스바': [250, 100], '월드콘': [500, 7]}
In [50]:
# 095 딕셔너리 keys() 메서드
# 다음의 딕셔너리로부터 key 값으로만 구성된 리스트를 생성하라.
icecream = {'탱크보이': 1200, '폴라포': 1200, '빵빠레': 1800, '월드콘': 1500, '메로나': 1000}
name = list(icecream.keys())
print(name)
['탱크보이', '폴라포', '빵빠레', '월드콘', '메로나']
In [51]:
# 096 딕셔너리 values() 메서드
# 다음의 딕셔너리에서 values 값으로만 구성된 리스트를 생성하라.
icecream = {'탱크보이': 1200, '폴라포': 1200, '빵빠레': 1800, '월드콘': 1500, '메로나': 1000}
price = list(icecream.values())
print(price)
[1200, 1200, 1800, 1500, 1000]
In [52]:
# 097 딕셔너리 values() 메서드
# icecream 딕셔너리에서 아이스크림 판매 금액의 총합을 출력하라.
icecream = {'탱크보이': 1200, '폴라포': 1200, '빵빠레': 1800, '월드콘': 1500, '메로나': 1000}
price = list(icecream.values())
print(sum(price))
6700
In [53]:
# 098 딕셔너리 update 메서드
# 아래의 new_product 딕셔너리를 다음 icecream 딕셔너리에 추가하라.
icecream = {'탱크보이': 1200, '폴라포': 1200, '빵빠레': 1800, '월드콘': 1500, '메로나': 1000}
new_product = {'팥빙수':2700, '아맛나':1000}
icecream.update(new_product)
print(icecream)
{'탱크보이': 1200, '폴라포': 1200, '빵빠레': 1800, '월드콘': 1500, '메로나': 1000, '팥빙수': 2700, '아맛나': 1000}
In [54]:
# 099 zip과 dict
# 아래 두 개의 튜플을 하나의 딕셔너리로 변환하라.
# keys를 키로, vals를 값으로 result 이름의 딕셔너리로 저장한다.
keys = ("apple", "pear", "peach")
vals = (300, 250, 400)
result = dict(zip(keys, vals))
print(result)
{'apple': 300, 'pear': 250, 'peach': 400}
In [55]:
# 100 zip과 dict
# date와 close_price 두 개의 리스트를 close_table 이름의 딕셔너리로 생성하라.
date = ['09/05', '09/06', '09/07', '09/08', '09/09']
close_price = [10500, 10300, 10100, 10800, 11000]
close_table = dict(zip(date, close_price))
print(close_table)
{'09/05': 10500, '09/06': 10300, '09/07': 10100, '09/08': 10800, '09/09': 11000}
In [56]:
# 서로 개수가 맞지 않으면 잘라버린다.
date = ['09/05', '09/06', '09/07', '09/08', '09/09']
close_price = [10500, 10300, 10100, 10800, 11000, 12000, 13100, 14583]
close_table = dict(zip(date, close_price))
print(close_table)
{'09/05': 10500, '09/06': 10300, '09/07': 10100, '09/08': 10800, '09/09': 11000}
pandas cheat sheet¶
In [57]:
import pandas as pd
Series¶
In [58]:
s = pd.Series([3, -5, 7, 4], index=['a', 'b', 'c', 'd'])
s
Out[58]:
a 3 b -5 c 7 d 4 dtype: int64
In [59]:
print(s[1])
print(s['b'])
-5 -5
In [60]:
s.drop(['a', 'c'])
Out[60]:
b -5 d 4 dtype: int64
DataFrame¶
In [61]:
data = {'Country': ['Belgium', 'India', 'Brazil'],
'Capital': ['Brussels', 'New Delhi', 'Brasilia'],
'Population': [11190846, 1303171035, 207847528]}
df0 = pd.DataFrame(data)
df = pd.DataFrame(data, columns=['Country', 'Capital', 'Population'])
print(df0)
print()
print(df)
Country Capital Population 0 Belgium Brussels 11190846 1 India New Delhi 1303171035 2 Brazil Brasilia 207847528 Country Capital Population 0 Belgium Brussels 11190846 1 India New Delhi 1303171035 2 Brazil Brasilia 207847528
In [62]:
# select single value by row&column
df.iloc[1, 1]
Out[62]:
'New Delhi'
In [63]:
# select single value by row&column labels
df.loc[2, 'Population']
Out[63]:
207847528
In [64]:
df.sort_values(by='Population')
Out[64]:
Country | Capital | Population | |
---|---|---|---|
0 | Belgium | Brussels | 11190846 |
2 | Brazil | Brasilia | 207847528 |
1 | India | New Delhi | 1303171035 |
In [65]:
df.shape
Out[65]:
(3, 3)
In [66]:
df.index
Out[66]:
RangeIndex(start=0, stop=3, step=1)
In [67]:
df.columns
Out[67]:
Index(['Country', 'Capital', 'Population'], dtype='object')
In [68]:
df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 3 entries, 0 to 2 Data columns (total 3 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Country 3 non-null object 1 Capital 3 non-null object 2 Population 3 non-null int64 dtypes: int64(1), object(2) memory usage: 200.0+ bytes
In [69]:
df.count()
Out[69]:
Country 3 Capital 3 Population 3 dtype: int64
In [70]:
df.sum()
Out[70]:
Country BelgiumIndiaBrazil Capital BrusselsNew DelhiBrasilia Population 1522209409 dtype: object
In [71]:
df.cumsum()
Out[71]:
Country | Capital | Population | |
---|---|---|---|
0 | Belgium | Brussels | 11190846 |
1 | BelgiumIndia | BrusselsNew Delhi | 1314361881 |
2 | BelgiumIndiaBrazil | BrusselsNew DelhiBrasilia | 1522209409 |
In [72]:
df.min()
Out[72]:
Country Belgium Capital Brasilia Population 11190846 dtype: object
In [73]:
df.max()
Out[73]:
Country India Capital New Delhi Population 1303171035 dtype: object
In [74]:
df.describe()
Out[74]:
Population | |
---|---|
count | 3.000000e+00 |
mean | 5.074031e+08 |
std | 6.961346e+08 |
min | 1.119085e+07 |
25% | 1.095192e+08 |
50% | 2.078475e+08 |
75% | 7.555093e+08 |
max | 1.303171e+09 |
In [75]:
# df.apply(lambda x : 수행문) : dataframe의 모든 요소들에 대해 함수를 적용하라!
df.apply(lambda x : x*2)
Out[75]:
Country | Capital | Population | |
---|---|---|---|
0 | BelgiumBelgium | BrusselsBrussels | 22381692 |
1 | IndiaIndia | New DelhiNew Delhi | 2606342070 |
2 | BrazilBrazil | BrasiliaBrasilia | 415695056 |
pandas 10 minutes + @¶
In [76]:
import numpy as np
Object creation¶
In [77]:
s = pd.Series([1, 3, 5, np.nan, 6, 8])
s
Out[77]:
0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float64
In [78]:
dates = pd.date_range('20210101', periods=6)
dates
Out[78]:
DatetimeIndex(['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05', '2021-01-06'], dtype='datetime64[ns]', freq='D')
In [79]:
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
df
Out[79]:
A | B | C | D | |
---|---|---|---|---|
2021-01-01 | -0.357479 | 1.842220 | 1.042668 | 1.830576 |
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | -0.709787 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | -1.596772 |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | -0.500373 |
2021-01-05 | -0.498451 | 0.024202 | 1.882293 | 1.712539 |
2021-01-06 | 0.376823 | 0.461702 | -0.783204 | -1.124303 |
In [80]:
df2 = pd.DataFrame({'A':1,
'B':pd.Timestamp('20130102'),
'C':pd.Series(1, index=list(range(4)), dtype='float32'),
'D':np.array([3]*4, dtype='int32'),
'E':pd.Categorical(["test", "train", "test", "train"]),
'F':'foo'})
df2
Out[80]:
A | B | C | D | E | F | |
---|---|---|---|---|---|---|
0 | 1 | 2013-01-02 | 1.0 | 3 | test | foo |
1 | 1 | 2013-01-02 | 1.0 | 3 | train | foo |
2 | 1 | 2013-01-02 | 1.0 | 3 | test | foo |
3 | 1 | 2013-01-02 | 1.0 | 3 | train | foo |
In [81]:
df2.dtypes
Out[81]:
A int64 B datetime64[ns] C float32 D int32 E category F object dtype: object
Viewing data¶
In [82]:
df.head()
Out[82]:
A | B | C | D | |
---|---|---|---|---|
2021-01-01 | -0.357479 | 1.842220 | 1.042668 | 1.830576 |
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | -0.709787 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | -1.596772 |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | -0.500373 |
2021-01-05 | -0.498451 | 0.024202 | 1.882293 | 1.712539 |
In [83]:
df.tail()
Out[83]:
A | B | C | D | |
---|---|---|---|---|
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | -0.709787 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | -1.596772 |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | -0.500373 |
2021-01-05 | -0.498451 | 0.024202 | 1.882293 | 1.712539 |
2021-01-06 | 0.376823 | 0.461702 | -0.783204 | -1.124303 |
In [84]:
df.values
Out[84]:
array([[-0.35747892, 1.84222038, 1.04266799, 1.83057578], [ 0.22842443, -0.5698502 , 0.55330847, -0.70978667], [ 0.55535103, -1.07795655, -1.25710216, -1.59677235], [ 1.37395172, -0.53598444, -0.42743514, -0.50037348], [-0.49845081, 0.02420241, 1.88229335, 1.71253887], [ 0.37682269, 0.46170238, -0.78320446, -1.12430274]])
In [85]:
df.T
Out[85]:
2021-01-01 | 2021-01-02 | 2021-01-03 | 2021-01-04 | 2021-01-05 | 2021-01-06 | |
---|---|---|---|---|---|---|
A | -0.357479 | 0.228424 | 0.555351 | 1.373952 | -0.498451 | 0.376823 |
B | 1.842220 | -0.569850 | -1.077957 | -0.535984 | 0.024202 | 0.461702 |
C | 1.042668 | 0.553308 | -1.257102 | -0.427435 | 1.882293 | -0.783204 |
D | 1.830576 | -0.709787 | -1.596772 | -0.500373 | 1.712539 | -1.124303 |
In [86]:
df.sort_index(axis=1, ascending=False)
Out[86]:
D | C | B | A | |
---|---|---|---|---|
2021-01-01 | 1.830576 | 1.042668 | 1.842220 | -0.357479 |
2021-01-02 | -0.709787 | 0.553308 | -0.569850 | 0.228424 |
2021-01-03 | -1.596772 | -1.257102 | -1.077957 | 0.555351 |
2021-01-04 | -0.500373 | -0.427435 | -0.535984 | 1.373952 |
2021-01-05 | 1.712539 | 1.882293 | 0.024202 | -0.498451 |
2021-01-06 | -1.124303 | -0.783204 | 0.461702 | 0.376823 |
In [87]:
df.sort_index(ascending=False)
Out[87]:
A | B | C | D | |
---|---|---|---|---|
2021-01-06 | 0.376823 | 0.461702 | -0.783204 | -1.124303 |
2021-01-05 | -0.498451 | 0.024202 | 1.882293 | 1.712539 |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | -0.500373 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | -1.596772 |
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | -0.709787 |
2021-01-01 | -0.357479 | 1.842220 | 1.042668 | 1.830576 |
Selection¶
In [88]:
# (1) Getting
df['A']
Out[88]:
2021-01-01 -0.357479 2021-01-02 0.228424 2021-01-03 0.555351 2021-01-04 1.373952 2021-01-05 -0.498451 2021-01-06 0.376823 Freq: D, Name: A, dtype: float64
In [89]:
df['2021-01-02':'2021-01-05']
Out[89]:
A | B | C | D | |
---|---|---|---|---|
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | -0.709787 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | -1.596772 |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | -0.500373 |
2021-01-05 | -0.498451 | 0.024202 | 1.882293 | 1.712539 |
In [90]:
# (2) Selection by label : loc[]
dates2 = pd.date_range('20210101', periods=6)
df.loc[dates[1]]
Out[90]:
A 0.228424 B -0.569850 C 0.553308 D -0.709787 Name: 2021-01-02 00:00:00, dtype: float64
In [91]:
df.loc[:, ['A', 'C']]
Out[91]:
A | C | |
---|---|---|
2021-01-01 | -0.357479 | 1.042668 |
2021-01-02 | 0.228424 | 0.553308 |
2021-01-03 | 0.555351 | -1.257102 |
2021-01-04 | 1.373952 | -0.427435 |
2021-01-05 | -0.498451 | 1.882293 |
2021-01-06 | 0.376823 | -0.783204 |
In [92]:
df.loc['2021-01-02':'2021-01-04', 'A':'C']
Out[92]:
A | B | C | |
---|---|---|---|
2021-01-02 | 0.228424 | -0.569850 | 0.553308 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 |
In [93]:
df.iloc[3]
Out[93]:
A 1.373952 B -0.535984 C -0.427435 D -0.500373 Name: 2021-01-04 00:00:00, dtype: float64
In [94]:
df.iloc[3:5, 1:3]
Out[94]:
B | C | |
---|---|---|
2021-01-04 | -0.535984 | -0.427435 |
2021-01-05 | 0.024202 | 1.882293 |
In [95]:
df.iloc[[1,2,4], [0,3]]
Out[95]:
A | D | |
---|---|---|
2021-01-02 | 0.228424 | -0.709787 |
2021-01-03 | 0.555351 | -1.596772 |
2021-01-05 | -0.498451 | 1.712539 |
In [96]:
df.iloc[1:3, :]
Out[96]:
A | B | C | D | |
---|---|---|---|---|
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | -0.709787 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | -1.596772 |
In [97]:
df.iloc[:, 1:3]
Out[97]:
B | C | |
---|---|---|
2021-01-01 | 1.842220 | 1.042668 |
2021-01-02 | -0.569850 | 0.553308 |
2021-01-03 | -1.077957 | -1.257102 |
2021-01-04 | -0.535984 | -0.427435 |
2021-01-05 | 0.024202 | 1.882293 |
2021-01-06 | 0.461702 | -0.783204 |
In [98]:
df.iloc[1, 1]
Out[98]:
-0.569850204401684
In [99]:
# (4) Boolean indexing(조건에 맞는 값 가져오기)
df[df['A'] > 0]
Out[99]:
A | B | C | D | |
---|---|---|---|---|
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | -0.709787 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | -1.596772 |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | -0.500373 |
2021-01-06 | 0.376823 | 0.461702 | -0.783204 | -1.124303 |
In [100]:
df[df > 0]
Out[100]:
A | B | C | D | |
---|---|---|---|---|
2021-01-01 | NaN | 1.842220 | 1.042668 | 1.830576 |
2021-01-02 | 0.228424 | NaN | 0.553308 | NaN |
2021-01-03 | 0.555351 | NaN | NaN | NaN |
2021-01-04 | 1.373952 | NaN | NaN | NaN |
2021-01-05 | NaN | 0.024202 | 1.882293 | 1.712539 |
2021-01-06 | 0.376823 | 0.461702 | NaN | NaN |
In [101]:
df2 = df.copy()
df2['E'] = ['one', 'one', 'two', 'three', 'four', 'three']
df2
Out[101]:
A | B | C | D | E | |
---|---|---|---|---|---|
2021-01-01 | -0.357479 | 1.842220 | 1.042668 | 1.830576 | one |
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | -0.709787 | one |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | -1.596772 | two |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | -0.500373 | three |
2021-01-05 | -0.498451 | 0.024202 | 1.882293 | 1.712539 | four |
2021-01-06 | 0.376823 | 0.461702 | -0.783204 | -1.124303 | three |
In [102]:
df2[df2['E'].isin(['two', 'four'])]
Out[102]:
A | B | C | D | E | |
---|---|---|---|---|---|
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | -1.596772 | two |
2021-01-05 | -0.498451 | 0.024202 | 1.882293 | 1.712539 | four |
In [103]:
# (5) Setting
s1 = pd.Series([1, 2, 3, 4, 5, 6], index=pd.date_range('20210102', periods=6))
s1
Out[103]:
2021-01-02 1 2021-01-03 2 2021-01-04 3 2021-01-05 4 2021-01-06 5 2021-01-07 6 Freq: D, dtype: int64
In [104]:
df['F'] = s1
df
Out[104]:
A | B | C | D | F | |
---|---|---|---|---|---|
2021-01-01 | -0.357479 | 1.842220 | 1.042668 | 1.830576 | NaN |
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | -0.709787 | 1.0 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | -1.596772 | 2.0 |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | -0.500373 | 3.0 |
2021-01-05 | -0.498451 | 0.024202 | 1.882293 | 1.712539 | 4.0 |
2021-01-06 | 0.376823 | 0.461702 | -0.783204 | -1.124303 | 5.0 |
In [105]:
# Setting by assigning with a NumPy array:
df.loc[:, 'D'] = np.array([5] * len(df))
df
Out[105]:
A | B | C | D | F | |
---|---|---|---|---|---|
2021-01-01 | -0.357479 | 1.842220 | 1.042668 | 5 | NaN |
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | 5 | 1.0 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | 5 | 2.0 |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | 5 | 3.0 |
2021-01-05 | -0.498451 | 0.024202 | 1.882293 | 5 | 4.0 |
2021-01-06 | 0.376823 | 0.461702 | -0.783204 | 5 | 5.0 |
In [106]:
df2 = df.copy()
df2[df2 > 0] = -df2
df2
Out[106]:
A | B | C | D | F | |
---|---|---|---|---|---|
2021-01-01 | -0.357479 | -1.842220 | -1.042668 | -5 | NaN |
2021-01-02 | -0.228424 | -0.569850 | -0.553308 | -5 | -1.0 |
2021-01-03 | -0.555351 | -1.077957 | -1.257102 | -5 | -2.0 |
2021-01-04 | -1.373952 | -0.535984 | -0.427435 | -5 | -3.0 |
2021-01-05 | -0.498451 | -0.024202 | -1.882293 | -5 | -4.0 |
2021-01-06 | -0.376823 | -0.461702 | -0.783204 | -5 | -5.0 |
Missing data¶
In [107]:
df1 = df.reindex(index=dates[0:4], columns=list(df.columns)+['E'])
df1
Out[107]:
A | B | C | D | F | E | |
---|---|---|---|---|---|---|
2021-01-01 | -0.357479 | 1.842220 | 1.042668 | 5 | NaN | NaN |
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | 5 | 1.0 | NaN |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | 5 | 2.0 | NaN |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | 5 | 3.0 | NaN |
In [108]:
df1.loc[dates[0]:dates[1], 'E'] = 1
df1
Out[108]:
A | B | C | D | F | E | |
---|---|---|---|---|---|---|
2021-01-01 | -0.357479 | 1.842220 | 1.042668 | 5 | NaN | 1.0 |
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | 5 | 1.0 | 1.0 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | 5 | 2.0 | NaN |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | 5 | 3.0 | NaN |
In [109]:
df1.dropna()
Out[109]:
A | B | C | D | F | E | |
---|---|---|---|---|---|---|
2021-01-02 | 0.228424 | -0.56985 | 0.553308 | 5 | 1.0 | 1.0 |
In [110]:
df1.fillna(value=5)
Out[110]:
A | B | C | D | F | E | |
---|---|---|---|---|---|---|
2021-01-01 | -0.357479 | 1.842220 | 1.042668 | 5 | 5.0 | 1.0 |
2021-01-02 | 0.228424 | -0.569850 | 0.553308 | 5 | 1.0 | 1.0 |
2021-01-03 | 0.555351 | -1.077957 | -1.257102 | 5 | 2.0 | 5.0 |
2021-01-04 | 1.373952 | -0.535984 | -0.427435 | 5 | 3.0 | 5.0 |
In [111]:
df1.isna()
Out[111]:
A | B | C | D | F | E | |
---|---|---|---|---|---|---|
2021-01-01 | False | False | False | False | True | False |
2021-01-02 | False | False | False | False | False | False |
2021-01-03 | False | False | False | False | False | True |
2021-01-04 | False | False | False | False | False | True |
Operations¶
In [112]:
# (1) Stats(통계 데이터)
df1.mean()
Out[112]:
A 0.450062 B -0.085393 C -0.022140 D 5.000000 F 2.000000 E 1.000000 dtype: float64
In [113]:
df1.mean(1)
Out[113]:
2021-01-01 1.705482 2021-01-02 1.201980 2021-01-03 1.044058 2021-01-04 1.682106 Freq: D, dtype: float64
In [114]:
s = pd.Series([1, 3, 5, np.nan, 6, 8], index=dates)
s
Out[114]:
2021-01-01 1.0 2021-01-02 3.0 2021-01-03 5.0 2021-01-04 NaN 2021-01-05 6.0 2021-01-06 8.0 Freq: D, dtype: float64
In [115]:
# shift는 이동
s = s.shift(2)
s
Out[115]:
2021-01-01 NaN 2021-01-02 NaN 2021-01-03 1.0 2021-01-04 3.0 2021-01-05 5.0 2021-01-06 NaN Freq: D, dtype: float64
In [116]:
# df의 각 값들에서 s를 뺀다
df.sub(s, axis='index')
Out[116]:
A | B | C | D | F | |
---|---|---|---|---|---|
2021-01-01 | NaN | NaN | NaN | NaN | NaN |
2021-01-02 | NaN | NaN | NaN | NaN | NaN |
2021-01-03 | -0.444649 | -2.077957 | -2.257102 | 4.0 | 1.0 |
2021-01-04 | -1.626048 | -3.535984 | -3.427435 | 2.0 | 0.0 |
2021-01-05 | -5.498451 | -4.975798 | -3.117707 | 0.0 | -1.0 |
2021-01-06 | NaN | NaN | NaN | NaN | NaN |
In [117]:
# (2) Apply
df.apply(np.cumsum)
Out[117]:
A | B | C | D | F | |
---|---|---|---|---|---|
2021-01-01 | -0.357479 | 1.842220 | 1.042668 | 5 | NaN |
2021-01-02 | -0.129054 | 1.272370 | 1.595976 | 10 | 1.0 |
2021-01-03 | 0.426297 | 0.194414 | 0.338874 | 15 | 3.0 |
2021-01-04 | 1.800248 | -0.341571 | -0.088561 | 20 | 6.0 |
2021-01-05 | 1.301797 | -0.317368 | 1.793733 | 25 | 10.0 |
2021-01-06 | 1.678620 | 0.144334 | 1.010528 | 30 | 15.0 |
In [118]:
df.cumsum()
Out[118]:
A | B | C | D | F | |
---|---|---|---|---|---|
2021-01-01 | -0.357479 | 1.842220 | 1.042668 | 5 | NaN |
2021-01-02 | -0.129054 | 1.272370 | 1.595976 | 10 | 1.0 |
2021-01-03 | 0.426297 | 0.194414 | 0.338874 | 15 | 3.0 |
2021-01-04 | 1.800248 | -0.341571 | -0.088561 | 20 | 6.0 |
2021-01-05 | 1.301797 | -0.317368 | 1.793733 | 25 | 10.0 |
2021-01-06 | 1.678620 | 0.144334 | 1.010528 | 30 | 15.0 |
In [119]:
df.apply(lambda x : x.max() - x.min())
Out[119]:
A 1.872403 B 2.920177 C 3.139396 D 0.000000 F 4.000000 dtype: float64
In [120]:
# (3) Histogramming
s = pd.Series(np.random.randint(0, 7, size=10))
s
Out[120]:
0 3 1 3 2 3 3 3 4 0 5 1 6 2 7 5 8 3 9 0 dtype: int32
In [121]:
print(s.count())
print(s.value_counts())
10 3 5 0 2 1 1 2 1 5 1 dtype: int64
In [122]:
df.value_counts()
Out[122]:
A B C D F -0.498451 0.024202 1.882293 5 4.0 1 0.228424 -0.569850 0.553308 5 1.0 1 0.376823 0.461702 -0.783204 5 5.0 1 0.555351 -1.077957 -1.257102 5 2.0 1 1.373952 -0.535984 -0.427435 5 3.0 1 dtype: int64
In [123]:
# (4) String Methods
s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])
s
Out[123]:
0 A 1 B 2 C 3 Aaba 4 Baca 5 NaN 6 CABA 7 dog 8 cat dtype: object
In [124]:
s.str.lower()
Out[124]:
0 a 1 b 2 c 3 aaba 4 baca 5 NaN 6 caba 7 dog 8 cat dtype: object
Merge¶
In [125]:
# (1) Concat
df = pd.DataFrame(np.random.randn(10, 4))
df
Out[125]:
0 | 1 | 2 | 3 | |
---|---|---|---|---|
0 | 1.061823 | -0.189506 | 0.508823 | -0.763932 |
1 | -0.166321 | -1.494115 | -1.350617 | -0.236430 |
2 | 0.581744 | 0.972317 | 1.206331 | -0.024456 |
3 | 1.625076 | 0.601253 | -1.071447 | -0.006372 |
4 | 0.696309 | -0.198812 | 0.481071 | 0.430160 |
5 | -0.450852 | 1.107708 | -0.595119 | 1.222581 |
6 | -0.629644 | 0.333313 | -0.506182 | -1.168663 |
7 | -1.073598 | 0.235184 | 0.509337 | -1.011330 |
8 | 1.603006 | 1.016855 | 0.261735 | 0.388407 |
9 | -0.409968 | -2.041778 | 0.522388 | 0.227021 |
In [126]:
pieces = [df[:3], df[3:7], df[7:]]
pieces
Out[126]:
[ 0 1 2 3 0 1.061823 -0.189506 0.508823 -0.763932 1 -0.166321 -1.494115 -1.350617 -0.236430 2 0.581744 0.972317 1.206331 -0.024456, 0 1 2 3 3 1.625076 0.601253 -1.071447 -0.006372 4 0.696309 -0.198812 0.481071 0.430160 5 -0.450852 1.107708 -0.595119 1.222581 6 -0.629644 0.333313 -0.506182 -1.168663, 0 1 2 3 7 -1.073598 0.235184 0.509337 -1.011330 8 1.603006 1.016855 0.261735 0.388407 9 -0.409968 -2.041778 0.522388 0.227021]
In [127]:
pd.concat(pieces)
Out[127]:
0 | 1 | 2 | 3 | |
---|---|---|---|---|
0 | 1.061823 | -0.189506 | 0.508823 | -0.763932 |
1 | -0.166321 | -1.494115 | -1.350617 | -0.236430 |
2 | 0.581744 | 0.972317 | 1.206331 | -0.024456 |
3 | 1.625076 | 0.601253 | -1.071447 | -0.006372 |
4 | 0.696309 | -0.198812 | 0.481071 | 0.430160 |
5 | -0.450852 | 1.107708 | -0.595119 | 1.222581 |
6 | -0.629644 | 0.333313 | -0.506182 | -1.168663 |
7 | -1.073598 | 0.235184 | 0.509337 | -1.011330 |
8 | 1.603006 | 1.016855 | 0.261735 | 0.388407 |
9 | -0.409968 | -2.041778 | 0.522388 | 0.227021 |
In [128]:
# (2) merge
left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})
left
Out[128]:
key | lval | |
---|---|---|
0 | foo | 1 |
1 | foo | 2 |
In [129]:
right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})
right
Out[129]:
key | rval | |
---|---|---|
0 | foo | 4 |
1 | foo | 5 |
In [130]:
pd.merge(left, right, on='key')
Out[130]:
key | lval | rval | |
---|---|---|---|
0 | foo | 1 | 4 |
1 | foo | 1 | 5 |
2 | foo | 2 | 4 |
3 | foo | 2 | 5 |
In [131]:
pd.merge(right, left, on='key')
Out[131]:
key | rval | lval | |
---|---|---|---|
0 | foo | 4 | 1 |
1 | foo | 4 | 2 |
2 | foo | 5 | 1 |
3 | foo | 5 | 2 |
In [132]:
left = pd.DataFrame({'key': ['foo', 'bar'], 'lval': [1, 2]})
left
Out[132]:
key | lval | |
---|---|---|
0 | foo | 1 |
1 | bar | 2 |
In [133]:
right = pd.DataFrame({'key': ['foo', 'bar'], 'rval': [4, 5]})
right
Out[133]:
key | rval | |
---|---|---|
0 | foo | 4 |
1 | bar | 5 |
In [134]:
pd.merge(left, right, on='key')
Out[134]:
key | lval | rval | |
---|---|---|---|
0 | foo | 1 | 4 |
1 | bar | 2 | 5 |
In [135]:
pd.merge(right, left, on='key')
Out[135]:
key | rval | lval | |
---|---|---|---|
0 | foo | 4 | 1 |
1 | bar | 5 | 2 |
Grouping¶
In [136]:
df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'three',
'two', 'two', 'one', 'three'],
'C': np.random.randn(8),
'D': np.random.randn(8)}
)
df
Out[136]:
A | B | C | D | |
---|---|---|---|---|
0 | foo | one | 0.857877 | -1.659561 |
1 | bar | one | -0.201005 | -0.889734 |
2 | foo | two | -0.884274 | -0.295573 |
3 | bar | three | 0.762701 | 1.502002 |
4 | foo | two | 0.903834 | -0.346878 |
5 | bar | two | 0.767651 | -0.867291 |
6 | foo | one | 1.389330 | -1.799274 |
7 | foo | three | 0.535811 | 0.210887 |
In [137]:
df.groupby('A').sum()
Out[137]:
C | D | |
---|---|---|
A | ||
bar | 1.329347 | -0.255023 |
foo | 2.802578 | -3.890399 |
In [138]:
df.groupby(['A', 'B']).sum()
Out[138]:
C | D | ||
---|---|---|---|
A | B | ||
bar | one | -0.201005 | -0.889734 |
three | 0.762701 | 1.502002 | |
two | 0.767651 | -0.867291 | |
foo | one | 2.247207 | -3.458835 |
three | 0.535811 | 0.210887 | |
two | 0.019560 | -0.642451 |
Reshaping¶
In [139]:
# (1) stack
list(zip([
['bar', 'bar', 'baz', 'baz','foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two','one', 'two', 'one', 'two']
]))
Out[139]:
[(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],), (['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'],)]
In [140]:
tuples = list(zip(*[['bar', 'bar', 'baz', 'baz','foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two','one', 'two', 'one', 'two']]))
tuples
Out[140]:
[('bar', 'one'), ('bar', 'two'), ('baz', 'one'), ('baz', 'two'), ('foo', 'one'), ('foo', 'two'), ('qux', 'one'), ('qux', 'two')]
In [141]:
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
index
Out[141]:
MultiIndex([('bar', 'one'), ('bar', 'two'), ('baz', 'one'), ('baz', 'two'), ('foo', 'one'), ('foo', 'two'), ('qux', 'one'), ('qux', 'two')], names=['first', 'second'])
In [142]:
df = pd.DataFrame(np.random.randn(8, 2), index=index, columns=['A', 'B'])
df
Out[142]:
A | B | ||
---|---|---|---|
first | second | ||
bar | one | -1.088797 | 0.524861 |
two | -0.751904 | 1.001665 | |
baz | one | 0.664458 | 0.298752 |
two | -0.928191 | 0.971767 | |
foo | one | 0.502834 | -1.027224 |
two | -0.279076 | -2.027543 | |
qux | one | 0.849527 | -0.075728 |
two | -0.714455 | -1.334737 |
In [143]:
df2 = df[:4]
df2
Out[143]:
A | B | ||
---|---|---|---|
first | second | ||
bar | one | -1.088797 | 0.524861 |
two | -0.751904 | 1.001665 | |
baz | one | 0.664458 | 0.298752 |
two | -0.928191 | 0.971767 |
In [144]:
# stack()을 하면 데이터를 쌓아서 보여줌
stacked = df2.stack()
stacked
Out[144]:
first second bar one A -1.088797 B 0.524861 two A -0.751904 B 1.001665 baz one A 0.664458 B 0.298752 two A -0.928191 B 0.971767 dtype: float64
In [145]:
# unstack()를 하면 원상복귀
stacked.unstack()
Out[145]:
A | B | ||
---|---|---|---|
first | second | ||
bar | one | -1.088797 | 0.524861 |
two | -0.751904 | 1.001665 | |
baz | one | 0.664458 | 0.298752 |
two | -0.928191 | 0.971767 |
In [146]:
stacked.unstack(1)
Out[146]:
second | one | two | |
---|---|---|---|
first | |||
bar | A | -1.088797 | -0.751904 |
B | 0.524861 | 1.001665 | |
baz | A | 0.664458 | -0.928191 |
B | 0.298752 | 0.971767 |
In [147]:
stacked.unstack(0)
Out[147]:
first | bar | baz | |
---|---|---|---|
second | |||
one | A | -1.088797 | 0.664458 |
B | 0.524861 | 0.298752 | |
two | A | -0.751904 | -0.928191 |
B | 1.001665 | 0.971767 |
In [148]:
# (2) pivot tables
df = pd.DataFrame({'A': ['one', 'one', 'two', 'three'] * 3,
'B': ['A', 'B', 'C'] * 4,
'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 2,
'D': np.random.randn(12),
'E': np.random.randn(12)})
df
Out[148]:
A | B | C | D | E | |
---|---|---|---|---|---|
0 | one | A | foo | -2.423209 | 1.428864 |
1 | one | B | foo | -0.454730 | -0.534660 |
2 | two | C | foo | -1.682026 | -1.710764 |
3 | three | A | bar | 0.894572 | -2.157935 |
4 | one | B | bar | 0.475995 | 0.956989 |
5 | one | C | bar | -0.517890 | -1.935444 |
6 | two | A | foo | -0.696429 | -2.015606 |
7 | three | B | foo | 0.010003 | -0.949925 |
8 | one | C | foo | -0.540024 | -1.865899 |
9 | one | A | bar | -0.006918 | -0.537639 |
10 | two | B | bar | 0.241807 | -0.358037 |
11 | three | C | bar | -0.883745 | -0.453944 |
In [149]:
pd.pivot_table(df, values='D', index=['A', 'B'], columns='C')
Out[149]:
C | bar | foo | |
---|---|---|---|
A | B | ||
one | A | -0.006918 | -2.423209 |
B | 0.475995 | -0.454730 | |
C | -0.517890 | -0.540024 | |
three | A | 0.894572 | NaN |
B | NaN | 0.010003 | |
C | -0.883745 | NaN | |
two | A | NaN | -0.696429 |
B | 0.241807 | NaN | |
C | NaN | -1.682026 |
In [150]:
pd.pivot_table(df, values='D', index=['B'], columns=['A', 'C'])
Out[150]:
A | one | three | two | |||
---|---|---|---|---|---|---|
C | bar | foo | bar | foo | bar | foo |
B | ||||||
A | -0.006918 | -2.423209 | 0.894572 | NaN | NaN | -0.696429 |
B | 0.475995 | -0.454730 | NaN | 0.010003 | 0.241807 | NaN |
C | -0.517890 | -0.540024 | -0.883745 | NaN | NaN | -1.682026 |
In [151]:
pd.pivot_table(df, values=['D', 'E'], index=['A', 'B'], columns=['C'])
Out[151]:
D | E | ||||
---|---|---|---|---|---|
C | bar | foo | bar | foo | |
A | B | ||||
one | A | -0.006918 | -2.423209 | -0.537639 | 1.428864 |
B | 0.475995 | -0.454730 | 0.956989 | -0.534660 | |
C | -0.517890 | -0.540024 | -1.935444 | -1.865899 | |
three | A | 0.894572 | NaN | -2.157935 | NaN |
B | NaN | 0.010003 | NaN | -0.949925 | |
C | -0.883745 | NaN | -0.453944 | NaN | |
two | A | NaN | -0.696429 | NaN | -2.015606 |
B | 0.241807 | NaN | -0.358037 | NaN | |
C | NaN | -1.682026 | NaN | -1.710764 |
엑셀 다루는 판다스¶
In [152]:
# 새 column 추가하기 (맨 마지막에 컬럼 추가)
df = pd.DataFrame(np.arange(16).reshape(4,4),
index=None,
columns=['price', 'qty', 'price', 'qty'])
df
Out[152]:
price | qty | price | qty | |
---|---|---|---|---|
0 | 0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 | 7 |
2 | 8 | 9 | 10 | 11 |
3 | 12 | 13 | 14 | 15 |
In [153]:
df['name'] = '-'
df
Out[153]:
price | qty | price | qty | name | |
---|---|---|---|---|---|
0 | 0 | 1 | 2 | 3 | - |
1 | 4 | 5 | 6 | 7 | - |
2 | 8 | 9 | 10 | 11 | - |
3 | 12 | 13 | 14 | 15 | - |
In [154]:
# 원하는 위치에 컬럼 추가하기
df = pd.DataFrame(np.arange(16).reshape(4,4),
index=None,
columns=['price', 'qty', 'price', 'qty'])
df.insert(2, 'name', '-')
df
Out[154]:
price | qty | name | price | qty | |
---|---|---|---|---|---|
0 | 0 | 1 | - | 2 | 3 |
1 | 4 | 5 | - | 6 | 7 |
2 | 8 | 9 | - | 10 | 11 |
3 | 12 | 13 | - | 14 | 15 |
In [155]:
# 원하는 위치에 컬럼 추가하기
df = pd.DataFrame(np.arange(16).reshape(4,4),
index=None,
columns=['price', 'qty', 'price', 'qty'])
df.insert(2, 'name', '-', allow_duplicates=True)
df
Out[155]:
price | qty | name | price | qty | |
---|---|---|---|---|---|
0 | 0 | 1 | - | 2 | 3 |
1 | 4 | 5 | - | 6 | 7 |
2 | 8 | 9 | - | 10 | 11 |
3 | 12 | 13 | - | 14 | 15 |
In [156]:
df.insert(2, 'name', '-', allow_duplicates=True)
df
Out[156]:
price | qty | name | name | price | qty | |
---|---|---|---|---|---|---|
0 | 0 | 1 | - | - | 2 | 3 |
1 | 4 | 5 | - | - | 6 | 7 |
2 | 8 | 9 | - | - | 10 | 11 |
3 | 12 | 13 | - | - | 14 | 15 |
In [157]:
# 기본 테이블을 멀티 컬럼, 인덱스로 바꾸기
df1 = pd.DataFrame(np.arange(16).reshape(4,4),
index=None,
columns=['price', 'qty', 'price', 'qty'])
df2 = pd.DataFrame(df1.values,
index = df1.index,
columns = [['apple','apple','banana','banana'], df1.columns]) # 2중 칼럼으로 만들기
df2
Out[157]:
apple | banana | |||
---|---|---|---|---|
price | qty | price | qty | |
0 | 0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 | 7 |
2 | 8 | 9 | 10 | 11 |
3 | 12 | 13 | 14 | 15 |
In [158]:
df1 = pd.DataFrame(np.arange(16).reshape(4,4),
index=None,
columns=['price', 'qty', 'price', 'qty'])
df2 = pd.DataFrame(df1.values,
index = df1.index,
columns = [df1.columns ,['apple','apple','banana','banana']]) # 2중 칼럼으로 만들기
df2
Out[158]:
price | qty | price | qty | |
---|---|---|---|---|
apple | apple | banana | banana | |
0 | 0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 | 7 |
2 | 8 | 9 | 10 | 11 |
3 | 12 | 13 | 14 | 15 |
In [159]:
# (이름으로) 행,열 삭제
df = pd.DataFrame(np.arange(16).reshape(4,4),
index=None,
columns=['price', 'qty', 'price', 'qty'])
df2 = df.drop(['price'], axis=1)
df2
Out[159]:
qty | qty | |
---|---|---|
0 | 1 | 3 |
1 | 5 | 7 |
2 | 9 | 11 |
3 | 13 | 15 |
In [160]:
df2 = df.drop([1, 2], axis=0)
df2
Out[160]:
price | qty | price | qty | |
---|---|---|---|---|
0 | 0 | 1 | 2 | 3 |
3 | 12 | 13 | 14 | 15 |
In [161]:
# (인덱스로) n번째 행 삭제
df = pd.DataFrame(np.arange(16).reshape(4,4),
index=None,
columns=['price', 'qty', 'price', 'qty'])
df2 = df.drop(df.index[1::2], axis=0)
df2
Out[161]:
price | qty | price | qty | |
---|---|---|---|---|
0 | 0 | 1 | 2 | 3 |
2 | 8 | 9 | 10 | 11 |
In [162]:
# 마지막 행에 컬럼 별 합계 삽입
df = pd.DataFrame(np.arange(6).reshape(2,3),
index=None,
columns=['price', 'qty', 'like'])
df.loc['합계'] = [ df[df.columns[x]].sum() for x in range(0, len(df.columns)) ]
df
Out[162]:
price | qty | like | |
---|---|---|---|
0 | 0 | 1 | 2 |
1 | 3 | 4 | 5 |
합계 | 3 | 5 | 7 |
In [163]:
# 튜플을 데이터프레임으로
data = [
('p1', 't1', 1, 2),
('p1', 't2', 3, 4),
('p2', 't1', 5, 6),
('p2', 't2', 7, 8),
('p2', 't3', 2, 8)
]
print(type(data))
df = pd.DataFrame(data)
df
<class 'list'>
Out[163]:
0 | 1 | 2 | 3 | |
---|---|---|---|---|
0 | p1 | t1 | 1 | 2 |
1 | p1 | t2 | 3 | 4 |
2 | p2 | t1 | 5 | 6 |
3 | p2 | t2 | 7 | 8 |
4 | p2 | t3 | 2 | 8 |
Ch12 예외 처리와 파일¶
예외 처리 구문¶
In [164]:
# try-except문
for i in range(10):
try: # if 와 달리 조건이 없다
print(10 / i)
except ZeroDivisionError: # 굳이 조건문 안 써도 된다
print("Not divided by 0")
Not divided by 0 10.0 5.0 3.3333333333333335 2.5 2.0 1.6666666666666667 1.4285714285714286 1.25 1.1111111111111112
In [165]:
# try-except-else문
for i in range(10):
try:
result = 10 / i
except ZeroDivisionError:
print('Not divided by 0')
else:
print(10 / i)
Not divided by 0 10.0 5.0 3.3333333333333335 2.5 2.0 1.6666666666666667 1.4285714285714286 1.25 1.1111111111111112
In [166]:
# try-except-finally문
try:
for i in range(1, 10):
result =10 // i
print(result)
except ZeroDivisionError:
print("Not divided by 0")
finally:
print("종료되었다.")
10 5 3 2 2 1 1 1 1 종료되었다.
In [167]:
# raise문
while True:
value = input("변환할 정수값을 입력해 주세요: ")
for digit in value:
if digit not in "0123456789":
raise ValueError("숫자값을 입력하지 않았습니다.")
print("정수값으로 변환된 숫자: ", int(value))
변환할 정수값을 입력해 주세요: 64 정수값으로 변환된 숫자: 64 변환할 정수값을 입력해 주세요: hi
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_8324/1784280802.py in <module> 4 for digit in value: 5 if digit not in "0123456789": ----> 6 raise ValueError("숫자값을 입력하지 않았습니다.") 7 print("정수값으로 변환된 숫자: ", int(value)) ValueError: 숫자값을 입력하지 않았습니다.
In [168]:
value = input("변환할 정수값을 입력해 주세요: ")
for digit in value:
print(digit)
변환할 정수값을 입력해 주세요: 64 6 4
In [169]:
# asssert문
def get_binary_nmubmer(decimal_number):
assert isinstance(decimal_number, int) # 입력값이 특정 클래스의 인스턴스인지
return bin(decimal_number)
print(get_binary_nmubmer(10))
0b1010
In [170]:
print(get_binary_nmubmer("10"))
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_8324/2104025875.py in <module> ----> 1 print(get_binary_nmubmer("10")) ~\AppData\Local\Temp/ipykernel_8324/2916046900.py in get_binary_nmubmer(decimal_number) 1 # asssert문 2 def get_binary_nmubmer(decimal_number): ----> 3 assert isinstance(decimal_number, int) # 입력값이 특정 클래스의 인스턴스인지 4 return bin(decimal_number) 5 print(get_binary_nmubmer(10)) AssertionError:
파일 다루기¶
파일 읽기¶
In [171]:
pwd
Out[171]:
'C:\\Users\\KJW\\Desktop\\python_basic'
In [172]:
# 읽기 모드로 "dream.txt" 텍스트 파일 열기
f = open("C:\\Users\\KJW\\Desktop\\python_basic\\resources\\dream.txt", "r", encoding='utf8') # 파일 객체 f에 파일 정보 저장
contents = f.read() # read() 함수로 해당 파일의 텍스트를 읽어서 변수에 저장
print(contents) # 저장된 변수 출력
f.close() # close() 함수로 파일 종료
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_8324/1721252459.py in <module> 1 # 읽기 모드로 "dream.txt" 텍스트 파일 열기 ----> 2 f = open("C:\\Users\\KJW\\Desktop\\python_basic\\resources\\dream.txt", "r", encoding='utf8') # 파일 객체 f에 파일 정보 저장 3 contents = f.read() # read() 함수로 해당 파일의 텍스트를 읽어서 변수에 저장 4 print(contents) # 저장된 변수 출력 5 f.close() # close() 함수로 파일 종료 FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\KJW\\Desktop\\python_basic\\resources\\dream.txt'
In [ ]:
# with문 + open 함수로 파일 열기
with open("dream.txt","r") as my_file:
contents = my_file.read()
print(contents)
In [ ]:
# readlins() 함수로 한 줄씩 읽어서 리스트형으로 반환하기
with open("dream.txt","r") as my_file:
content_list = my_file.readlines() # 파일 전체를 리스트로 반환
print(type(content_list)) # 자료형 확인
print(content_list) # 리스트값 출력
In [ ]:
# readline() 함수로 실행할 때마다 한 줄씩 읽어오기
# 내용을 찾다가 중간에 특정 작업을 하거나 멈춰야 할 필요가 있는 대용량 데이터에서 사용
with open("dream.txt", "r") as my_file:
i = 0
while 1:
line = my_file.readline()
if not line: # 읽어온 줄에 내용이 없다면 파일 읽기 종료
break
print(str(i)+" === "+ line.replace("\n","")) # 한 줄씩 값 출력
i = i + 1
In [173]:
# 파일 안 글자의 통계 정보 출력하기
with open("dream.txt", "r") as my_file:
contents = my_file.read()
word_list = contents.split(" ") # 빈칸 기준으로 단어를 분리 리스트
line_list = contents.split("\n") # 한 줄씩 분리하여 리스트
print("총 글자의 수:", len(contents))
print("총 단어의 수:", len(word_list))
print("총 줄의 수:", len(line_list))
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_8324/3741326766.py in <module> 1 # 파일 안 글자의 통계 정보 출력하기 ----> 2 with open("dream.txt", "r") as my_file: 3 contents = my_file.read() 4 word_list = contents.split(" ") # 빈칸 기준으로 단어를 분리 리스트 5 line_list = contents.split("\n") # 한 줄씩 분리하여 리스트 FileNotFoundError: [Errno 2] No such file or directory: 'dream.txt'
파일 쓰기¶
In [174]:
f = open("C:\\Users\\KJW\\Desktop\\python_basic\\resources\\TextFile\\count_log.txt", 'w', encoding = "utf8") # utf-8-sig
for i in range(1, 5):
data = "%d번째 줄이다.\n" % i
print(data)
f.write(data)
f.close()
1번째 줄이다. 2번째 줄이다. 3번째 줄이다. 4번째 줄이다.
파일 추가 모드¶
In [175]:
with open("C:\\Users\\KJW\\Desktop\\python_basic\\resources\\TextFile\\count_log.txt", 'a', encoding = "utf8") as f:
for i in range(1, 5):
data = "%d번째 줄이다.\n" % i
print(data)
f.write(data)
1번째 줄이다. 2번째 줄이다. 3번째 줄이다. 4번째 줄이다.
디렉터리 만들기¶
In [176]:
# 기존 디렉터리가 없는지 확인하고 만들기
import os
if not os.path.isdir("log"):
os.mkdir("log")
로그파일 만들기¶
In [177]:
import os
# 로그 디렉토리 생성
if not os.path.isdir("log"):
os.mkdir("log")
# 기록 파일 생성 후 기록 시작 문구 저장
if not os.path.exists("log/count_log.txt"):
f = open("log/count_log.txt", 'w', encoding = "utf8")
f.write("기록이 시작된다.\n")
f.close()
# 시간(stamp)과 특정값(vlaue)을 기록한 후 저장
with open("log/count_log.txt", 'a', encoding = "utf8") as f:
import random, datetime
for i in range(1, 11):
stamp = str(datetime.datetime.now())
value = random.random() * 1000000
log_line = stamp + "\t" + str(value) + "값이 생성되었다." + "\n"
f.write(log_line)
Pickle 모듈¶
In [178]:
# pickle 파일로 저장하기 : 'wb' 모드
import pickle
f = open("log/list2.pickle", "wb")
test = [1,2,3,4,5, 'hi']
pickle.dump(test, f)
f.close()
In [179]:
# 저장된 pickle 파일 불러오기 : 'rb' 모드
f = open("log/list2.pickle", "rb")
list_pickle = pickle.load(f)
print(list_pickle)
f.close()
[1, 2, 3, 4, 5, 'hi']
클래스로 생성된 인스턴스를 pickle 파일로 저장하기¶
In [180]:
# 곱셈기 클래스
class Multiply(object):
def __init__(self, multiplier):
self.multiplier = multiplier
# 곱셈 메소드
def multiply(self, number):
return number * self.multiplier
In [181]:
# 인스턴스 생성
multiplier = Multiply(5)
In [182]:
multiplier.multiply(10)
Out[182]:
50
In [183]:
# 인스턴스를 pickle 파일로 저장하기
import pickle
f = open("log/multiply_object.pickle", "wb")
pickle.dump(multiplier, f)
f.close()
In [184]:
# 저장된 pickle 파일 읽기
f = open("log/multiply_object.pickle", "rb")
multiply_pickle = pickle.load(f)
multiply_pickle.multiply(10)
Out[184]:
50
Ch13 csv와 로그관리¶
교재 코드로 csv 파일 읽기¶
In [185]:
line_counter = 0 # 파일의 총 줄 수를 세는 변수
data_header = [ ] # 데이터의 필드값을 저장하는 리스트
customer_list = [ ] # customer의 개별 리스트를 저장하는 리스트
with open("resources/customers.csv") as customer_data: # customers.csv 파일을 customer_data 객체에 저장
while 1:
data = customer_data.readline() # customers.csv에 데이터 변수에 한 줄씩 저장
if not data:break # 데이터가 없을 때, 반복문 종료
if line_counter == 0: # 첫 번째 데이터는 데이터의 필드
data_header = data.split(",") # 데이터의 필드는 data_header 리스트에 저장, 데이터 저장 시 ","로 분리
else:
customer_list.append(data.split(",")) # 일반 데이터는 customer_list 객체에 저장, 데이터 저장 시 “,”로 분리
line_counter +=1
print("Header:", data_header) # 데이터 필드값 출력
for i in range(0, 10): # 데이터 출력(샘플 10개만)
print("Data",i,":",customer_list[i])
print(len(customer_list)) # 전체 데이터 크기 출력
Header: ['customerNumber', 'customerName', 'contactLastName', 'contactFirstName', 'phone', 'addressLine1', 'addressLine2', 'city', 'state', 'postalCode', 'country', 'salesRepEmployeeNumber', 'creditLimit\n'] Data 0 : ['103', 'Atelier graphique', 'Schmitt', 'Carine ', '40.32.2555', '"54', ' rue Royale"', 'NULL', 'Nantes', 'NULL', '44000', 'France', '1370', '21000\n'] Data 1 : ['112', 'Signal Gift Stores', 'King', 'Jean', '7025551838', '8489 Strong St.', 'NULL', 'Las Vegas', 'NV', '83030', 'USA', '1166', '71800\n'] Data 2 : ['114', '"Australian Collectors', ' Co."', 'Ferguson', 'Peter', '03 9520 4555', '636 St Kilda Road', 'Level 3', 'Melbourne', 'Victoria', '3004', 'Australia', '1611', '117300\n'] Data 3 : ['119', 'La Rochelle Gifts', 'Labrune', 'Janine ', '40.67.8555', '"67', ' rue des Cinquante Otages"', 'NULL', 'Nantes', 'NULL', '44000', 'France', '1370', '118200\n'] Data 4 : ['121', 'Baane Mini Imports', 'Bergulfsen', 'Jonas ', '07-98 9555', 'Erling Skakkes gate 78', 'NULL', 'Stavern', 'NULL', '4110', 'Norway', '1504', '81700\n'] Data 5 : ['124', 'Mini Gifts Distributors Ltd.', 'Nelson', 'Susan', '4155551450', '5677 Strong St.', 'NULL', 'San Rafael', 'CA', '97562', 'USA', '1165', '210500\n'] Data 6 : ['125', 'Havel & Zbyszek Co', 'Piestrzeniewicz', 'Zbyszek ', '(26) 642-7555', 'ul. Filtrowa 68', 'NULL', 'Warszawa', 'NULL', '01-012', 'Poland', 'NULL', '0\n'] Data 7 : ['128', '"Blauer See Auto', ' Co."', 'Keitel', 'Roland', '+49 69 66 90 2555', 'Lyonerstr. 34', 'NULL', 'Frankfurt', 'NULL', '60528', 'Germany', '1504', '59700\n'] Data 8 : ['129', 'Mini Wheels Co.', 'Murphy', 'Julie', '6505555787', '5557 North Pendale Street', 'NULL', 'San Francisco', 'CA', '94217', 'USA', '1165', '64600\n'] Data 9 : ['131', 'Land of Toys Inc.', 'Lee', 'Kwai', '2125557818', '897 Long Airport Avenue', 'NULL', 'NYC', 'NY', '10022', 'USA', '1323', '114900\n'] 122
In [186]:
# 판다스로 열기
import pandas as pd
df = pd.read_csv("resources/customers.csv")
print(df.shape)
df
(122, 13)
Out[186]:
customerNumber | customerName | contactLastName | contactFirstName | phone | addressLine1 | addressLine2 | city | state | postalCode | country | salesRepEmployeeNumber | creditLimit | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 103 | Atelier graphique | Schmitt | Carine | 40.32.2555 | 54, rue Royale | NaN | Nantes | NaN | 44000 | France | 1370.0 | 21000 |
1 | 112 | Signal Gift Stores | King | Jean | 7025551838 | 8489 Strong St. | NaN | Las Vegas | NV | 83030 | USA | 1166.0 | 71800 |
2 | 114 | Australian Collectors, Co. | Ferguson | Peter | 03 9520 4555 | 636 St Kilda Road | Level 3 | Melbourne | Victoria | 3004 | Australia | 1611.0 | 117300 |
3 | 119 | La Rochelle Gifts | Labrune | Janine | 40.67.8555 | 67, rue des Cinquante Otages | NaN | Nantes | NaN | 44000 | France | 1370.0 | 118200 |
4 | 121 | Baane Mini Imports | Bergulfsen | Jonas | 07-98 9555 | Erling Skakkes gate 78 | NaN | Stavern | NaN | 4110 | Norway | 1504.0 | 81700 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
117 | 486 | Motor Mint Distributors Inc. | Salazar | Rosa | 2155559857 | 11328 Douglas Av. | NaN | Philadelphia | PA | 71270 | USA | 1323.0 | 72600 |
118 | 487 | Signal Collectibles Ltd. | Taylor | Sue | 4155554312 | 2793 Furth Circle | NaN | Brisbane | CA | 94217 | USA | 1165.0 | 60300 |
119 | 489 | Double Decker Gift Stores, Ltd | Smith | Thomas | (171) 555-7555 | 120 Hanover Sq. | NaN | London | NaN | WA1 1DP | UK | 1501.0 | 43300 |
120 | 495 | Diecast Collectables | Franco | Valarie | 6175552555 | 6251 Ingle Ln. | NaN | Boston | MA | 51003 | USA | 1188.0 | 85100 |
121 | 496 | Kelly's Gift Shop | Snowden | Tony | +64 9 5555500 | Arenales 1938 3'A' | NaN | Auckland | NaN | NaN | New Zealand | 1612.0 | 110000 |
122 rows × 13 columns
로그 관리¶
In [187]:
import logging
logging.debug("틀렸잖아!")
logging.info("확인해!")
logging.warning("조심해!")
logging.error("에러 났어!!!")
logging.critical ("망했다….")
WARNING:root:조심해! ERROR:root:에러 났어!!! CRITICAL:root:망했다….
In [188]:
import logging
logger = logging.getLogger("main") # logger 객체에 로그를 기록할 예정
stream_handler = logging.StreamHandler() # StreamHandler : 출력 공간 지정
logger.addHandler(stream_handler) # logger 의 출력 등록
logger.setLevel(logging.CRITICAL) # 로깅 레벨 지정
logger.debug("틀렸잖아!")
logger.info("확인해!")
logger.warning("조심해!")
logger.error("에러 났어!!!")
logger.critical("망했다….")
망했다…. CRITICAL:main:망했다….
설정 저장¶
In [189]:
# configparser 모듈
import configparser
config = configparser.ConfigParser()
config.sections()
config.read("resources/example.cfg")
Out[189]:
['resources/example.cfg']
In [190]:
config.sections()
Out[190]:
['SectionOne', 'SectionTwo', 'SectionThree']
In [191]:
for key in config['SectionOne']:
print(key)
status name value age single
In [192]:
for key in config['SectionTwo']:
print(key)
favoritecolor
In [193]:
config['SectionOne']["age"]
Out[193]:
'30'
In [194]:
# argparse 모듈
# cmd 창에 이렇게 쳐보기
# C:\Users\KJW\Desktop\python_basic\resources>python arg_sum.py -a 5 -b 3
# cmd 창에 그냥 arg_sum.py 치면
# visual studio code 창으로 이동해서 뜬다.
'Data Science > 국비지원' 카테고리의 다른 글
Day11 (0) | 2021.12.28 |
---|---|
시험 대비 (21.12.27) (1) | 2021.12.27 |
Day10 (0) | 2021.12.24 |
Day9 - Code (1) | 2021.12.23 |
Day9 (0) | 2021.12.23 |