Coding Questions (1) - 카카오톡 여름 인턴 문제 1번
Coding Questions 구성
import pandas as pd
iphone_df = pd.read_csv('desktop/codeit/iphone.csv', index_col = 0)
iphone_df
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | |
---|---|---|---|---|---|
iPhone 7 | 2016-09-16 | 4.7 | 2GB | iOS 10.0 | No |
iPhone 7 Plus | 2016-09-16 | 5.5 | 3GB | iOS 10.0 | No |
iPhone 8 | 2017-09-22 | 4.7 | 2GB | iOS 11.0 | No |
iPhone 8 Plus | 2017-09-22 | 5.5 | 3GB | iOS 11.0 | No |
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 | Yes |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 | Yes |
iPhone XS Max | 2018-09-21 | 6.5 | 4GB | iOS 12.0 | Yes |
iphone_df.loc['iPhone 8', '메모리'] #iphone_df.loc[row, column]
'2GB'
# iphone_df.loc['iPhone 8', '가격'] 가격이라는 column이 존재하지않음
iphone_df.loc['iPhone X', :] #모든 columns
출시일 2017-11-03
디스플레이 5.8
메모리 3GB
출시 버전 iOS 11.1
Face ID Yes
Name: iPhone X, dtype: object
type(iphone_df.loc['iPhone X'])
pandas.core.series.Series
iphone_df.loc[:, '출시일']
iPhone 7 2016-09-16
iPhone 7 Plus 2016-09-16
iPhone 8 2017-09-22
iPhone 8 Plus 2017-09-22
iPhone X 2017-11-03
iPhone XS 2018-09-21
iPhone XS Max 2018-09-21
Name: 출시일, dtype: object
iphone_df['디스플레이'] #column부를때는 .loc따로 필요없음
iPhone 7 4.7
iPhone 7 Plus 5.5
iPhone 8 4.7
iPhone 8 Plus 5.5
iPhone X 5.8
iPhone XS 5.8
iPhone XS Max 6.5
Name: 디스플레이, dtype: float64
iphone_df
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | |
---|---|---|---|---|---|
iPhone 7 | 2016-09-16 | 4.7 | 2GB | iOS 10.0 | No |
iPhone 7 Plus | 2016-09-16 | 5.5 | 3GB | iOS 10.0 | No |
iPhone 8 | 2017-09-22 | 4.7 | 2GB | iOS 11.0 | No |
iPhone 8 Plus | 2017-09-22 | 5.5 | 3GB | iOS 11.0 | No |
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 | Yes |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 | Yes |
iPhone XS Max | 2018-09-21 | 6.5 | 4GB | iOS 12.0 | Yes |
#row 불러오기
iphone_df.loc['iPhone X'] #return pandas series
출시일 2017-11-03
디스플레이 5.8
메모리 3GB
출시 버전 iOS 11.1
Face ID Yes
Name: iPhone X, dtype: object
iphone_df.loc[['iPhone X', 'iPhone 8']]
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | |
---|---|---|---|---|---|
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 | Yes |
iPhone 8 | 2017-09-22 | 4.7 | 2GB | iOS 11.0 | No |
type(iphone_df.loc[['iPhone X', 'iPhone 8']])
#return pandas dataFrame
pandas.core.frame.DataFrame
#columns 불러오기
iphone_df[['Face ID', '출시일', '메모리']]
Face ID | 출시일 | 메모리 | |
---|---|---|---|
iPhone 7 | No | 2016-09-16 | 2GB |
iPhone 7 Plus | No | 2016-09-16 | 3GB |
iPhone 8 | No | 2017-09-22 | 2GB |
iPhone 8 Plus | No | 2017-09-22 | 3GB |
iPhone X | Yes | 2017-11-03 | 3GB |
iPhone XS | Yes | 2018-09-21 | 4GB |
iPhone XS Max | Yes | 2018-09-21 | 4GB |
#슬라이싱
iphone_df.loc['iPhone 8': 'iPhone XS']
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | |
---|---|---|---|---|---|
iPhone 8 | 2017-09-22 | 4.7 | 2GB | iOS 11.0 | No |
iPhone 8 Plus | 2017-09-22 | 5.5 | 3GB | iOS 11.0 | No |
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 | Yes |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 | Yes |
#tricky
iphone_df['메모리': 'Face ID']
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID |
---|
#column slicing
iphone_df.loc[:, "메모리": "Face ID"]
메모리 | 출시 버전 | Face ID | |
---|---|---|---|
iPhone 7 | 2GB | iOS 10.0 | No |
iPhone 7 Plus | 3GB | iOS 10.0 | No |
iPhone 8 | 2GB | iOS 11.0 | No |
iPhone 8 Plus | 3GB | iOS 11.0 | No |
iPhone X | 3GB | iOS 11.1 | Yes |
iPhone XS | 4GB | iOS 12.0 | Yes |
iPhone XS Max | 4GB | iOS 12.0 | Yes |
#with boolean for row
iphone_df.loc[[True, False, True, True, False, True, False]]
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | |
---|---|---|---|---|---|
iPhone 7 | 2016-09-16 | 4.7 | 2GB | iOS 10.0 | No |
iPhone 8 | 2017-09-22 | 4.7 | 2GB | iOS 11.0 | No |
iPhone 8 Plus | 2017-09-22 | 5.5 | 3GB | iOS 11.0 | No |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 | Yes |
#with boolean for column
iphone_df.loc[:, [True, False, False, True, False]]
출시일 | 출시 버전 | |
---|---|---|
iPhone 7 | 2016-09-16 | iOS 10.0 |
iPhone 7 Plus | 2016-09-16 | iOS 10.0 |
iPhone 8 | 2017-09-22 | iOS 11.0 |
iPhone 8 Plus | 2017-09-22 | iOS 11.0 |
iPhone X | 2017-11-03 | iOS 11.1 |
iPhone XS | 2018-09-21 | iOS 12.0 |
iPhone XS Max | 2018-09-21 | iOS 12.0 |
iphone_df['디스플레이'] > 5
iPhone 7 False
iPhone 7 Plus True
iPhone 8 False
iPhone 8 Plus True
iPhone X True
iPhone XS True
iPhone XS Max True
Name: 디스플레이, dtype: bool
iphone_df.loc[iphone_df['디스플레이'] > 5]
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | |
---|---|---|---|---|---|
iPhone 7 Plus | 2016-09-16 | 5.5 | 3GB | iOS 10.0 | No |
iPhone 8 Plus | 2017-09-22 | 5.5 | 3GB | iOS 11.0 | No |
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 | Yes |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 | Yes |
iPhone XS Max | 2018-09-21 | 6.5 | 4GB | iOS 12.0 | Yes |
iphone_df['Face ID'] == 'Yes'
iPhone 7 False
iPhone 7 Plus False
iPhone 8 False
iPhone 8 Plus False
iPhone X True
iPhone XS True
iPhone XS Max True
Name: Face ID, dtype: bool
iphone_df.loc[iphone_df['Face ID'] == 'Yes']
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | |
---|---|---|---|---|---|
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 | Yes |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 | Yes |
iPhone XS Max | 2018-09-21 | 6.5 | 4GB | iOS 12.0 | Yes |
condition = (iphone_df['Face ID'] == 'Yes') & (iphone_df['디스플레이'] > 5)
#2가지 묶을때는 괄호 쓰기
iphone_df.loc[condition]
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | |
---|---|---|---|---|---|
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 | Yes |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 | Yes |
iPhone XS Max | 2018-09-21 | 6.5 | 4GB | iOS 12.0 | Yes |
iphone_df
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | |
---|---|---|---|---|---|
iPhone 7 | 2016-09-16 | 4.7 | 2GB | iOS 10.0 | No |
iPhone 7 Plus | 2016-09-16 | 5.5 | 3GB | iOS 10.0 | No |
iPhone 8 | 2017-09-22 | 4.7 | 2GB | iOS 11.0 | No |
iPhone 8 Plus | 2017-09-22 | 5.5 | 3GB | iOS 11.0 | No |
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 | Yes |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 | Yes |
iPhone XS Max | 2018-09-21 | 6.5 | 4GB | iOS 12.0 | Yes |
#DataFrame 위치로 인덱싱하기
iphone_df.iloc[2, 4] #iloc[row, column]
'No'
iphone_df.iloc[[1,3], [1,4]]
디스플레이 | Face ID | |
---|---|---|
iPhone 7 Plus | 5.5 | No |
iPhone 8 Plus | 5.5 | No |
iphone_df.iloc[3:, 1:4]
디스플레이 | 메모리 | 출시 버전 | |
---|---|---|---|
iPhone 8 Plus | 5.5 | 3GB | iOS 11.0 |
iPhone X | 5.8 | 3GB | iOS 11.1 |
iPhone XS | 5.8 | 4GB | iOS 12.0 |
iPhone XS Max | 6.5 | 4GB | iOS 12.0 |
iphone_df
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | |
---|---|---|---|---|---|
iPhone 7 | 2016-09-16 | 4.7 | 2GB | iOS 10.0 | No |
iPhone 7 Plus | 2016-09-16 | 5.5 | 3GB | iOS 10.0 | No |
iPhone 8 | 2017-09-22 | 4.7 | 2GB | iOS 11.0 | No |
iPhone 8 Plus | 2017-09-22 | 5.5 | 3GB | iOS 11.0 | No |
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 | Yes |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 | Yes |
iPhone XS Max | 2018-09-21 | 6.5 | 4GB | iOS 12.0 | Yes |
iphone_df.loc['iPhone 7'] #return list of row
iphone_df.loc[['iPhone 7', 'iPhone 7 Plus']] #return dataFrame of rows
#same func row slicing
iphone_df.loc['iPhone 7' : 'iPhone X']
iphone_df['iPhone 7' : 'iPhone X']
#same func to get column
iphone_df.loc[:, '출시일']
iphone_df['출시일']
#same func to get specific columns
iphone_df.loc[:, ['출시일', '디스플레이']]
iphone_df[['출시일', '디스플레이']]
#column slicing
iphone_df.loc[:, "출시일":"출시 버전"]
출시일 | 디스플레이 | 메모리 | 출시 버전 | |
---|---|---|---|---|
iPhone 7 | 2016-09-16 | 4.7 | 2GB | iOS 10.0 |
iPhone 7 Plus | 2016-09-16 | 5.5 | 3GB | iOS 10.0 |
iPhone 8 | 2017-09-22 | 4.7 | 2GB | iOS 11.0 |
iPhone 8 Plus | 2017-09-22 | 5.5 | 3GB | iOS 11.0 |
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 |
iPhone XS Max | 2018-09-21 | 6.5 | 4GB | iOS 12.0 |
#one row location
iphone_df.iloc[0]
#multiple row locations
iphone_df.iloc[[1, 2, 3]]
#row slicing
iphone_df.iloc[1: 3]
iphone_df[1:3]
#one column location
iphone_df.iloc[:, 3]
#multiple column locations
iphone_df.iloc[:, [1, 2, 3]]
#column slicing
iphone_df.iloc[:, 0:3]
출시일 | 디스플레이 | 메모리 | |
---|---|---|---|
iPhone 7 | 2016-09-16 | 4.7 | 2GB |
iPhone 7 Plus | 2016-09-16 | 5.5 | 3GB |
iPhone 8 | 2017-09-22 | 4.7 | 2GB |
iPhone 8 Plus | 2017-09-22 | 5.5 | 3GB |
iPhone X | 2017-11-03 | 5.8 | 3GB |
iPhone XS | 2018-09-21 | 5.8 | 4GB |
iPhone XS Max | 2018-09-21 | 6.5 | 4GB |
iphone_df
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | 확인용 | |
---|---|---|---|---|---|---|
iPhone 7 | 1 | 1.0 | 1 | 1 | 1 | 100 |
iPhone 7 Plus | 2016-09-16 | 5.5 | 3GB | iOS 10.0 | No | 80 |
iPhone 8 | 1 | 1.0 | 1 | 1 | 1 | 81 |
iPhone 8 Plus | 2017-09-22 | 5.5 | 3GB | iOS 11.0 | No | 82 |
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 | Yes | 50 |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 | Yes | 40 |
iPhone XS Max | 2018-09-21 | 6.5 | 4GB | iOS 12.0 | Yes | 30 |
iphone_df['디스플레이'] < 5
iPhone 7 True
iPhone 7 Plus False
iPhone 8 True
iPhone 8 Plus False
iPhone X False
iPhone XS False
iPhone XS Max False
Name: 디스플레이, dtype: bool
iphone_df
출시일 | 디스플레이 | 메모리 | 출시 버전 | Face ID | 확인용 | |
---|---|---|---|---|---|---|
iPhone 7 | 2016-09-16 | 4.7 | 2GB | iOS 10.0 | No | 100 |
iPhone 7 Plus | 2016-09-16 | 5.5 | 3GB | iOS 10.0 | No | 80 |
iPhone 8 | 2017-09-22 | 4.7 | 2GB | iOS 11.0 | No | 81 |
iPhone 8 Plus | 2017-09-22 | 5.5 | 3GB | iOS 11.0 | No | 82 |
iPhone X | 2017-11-03 | 5.8 | 3GB | iOS 11.1 | Yes | 50 |
iPhone XS | 2018-09-21 | 5.8 | 4GB | iOS 12.0 | Yes | 40 |
iPhone XS Max | 2018-09-21 | 6.5 | 4GB | iOS 12.0 | Yes | 30 |
iphone_df[['디스플레이', '확인용']] > 5
디스플레이 | 확인용 | |
---|---|---|
iPhone 7 | False | True |
iPhone 7 Plus | True | True |
iPhone 8 | False | True |
iPhone 8 Plus | True | True |
iPhone X | True | True |
iPhone XS | True | True |
iPhone XS Max | True | True |
iphone_df.loc[:, ['디스플레이', '확인용']] > 5
디스플레이 | 확인용 | |
---|---|---|
iPhone 7 | False | True |
iPhone 7 Plus | True | True |
iPhone 8 | False | True |
iPhone 8 Plus | True | True |
iPhone X | True | True |
iPhone XS | True | True |
iPhone XS Max | True | True |