728x90
데이터 EDA를 위한 Pandas 역량기르기 타~임!
* 문제풀이 깃허브: https://github.com/LIMSONA/KDT/blob/main/pandas/Day_1/%EC%97%B0%EC%8A%B502.ipynb
Step 1. 필요한 라이브러리 임포트¶
In [1]:
import pandas as pd
import numpy as np
In [2]:
# Create an example dataframe about a fictional army
raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'],
'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'],
'deaths': [523, 52, 25, 616, 43, 234, 523, 62, 62, 73, 37, 35],
'battles': [5, 42, 2, 2, 4, 7, 8, 3, 4, 7, 8, 9],
'size': [1045, 957, 1099, 1400, 1592, 1006, 987, 849, 973, 1005, 1099, 1523],
'veterans': [1, 5, 62, 26, 73, 37, 949, 48, 48, 435, 63, 345],
'readiness': [1, 2, 3, 3, 2, 1, 2, 3, 2, 1, 2, 3],
'armored': [1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1],
'deserters': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
'origin': ['Arizona', 'California', 'Texas', 'Florida', 'Maine', 'Iowa', 'Alaska', 'Washington', 'Oregon', 'Wyoming', 'Louisana', 'Georgia']}
Step 3. 데이터 불러오기¶
raw_data를 pandas DataFrame형태로 임포트 후 army 변수에 대입해주세요
raw_data에 입력된 순서를 지키면서 데이터프레임을 생성해주세요. (regiment, company, deaths, ...)
In [13]:
army = pd.DataFrame(raw_data)
army
Out[13]:
regiment | company | deaths | battles | size | veterans | readiness | armored | deserters | origin | |
---|---|---|---|---|---|---|---|---|---|---|
0 | Nighthawks | 1st | 523 | 5 | 1045 | 1 | 1 | 1 | 4 | Arizona |
1 | Nighthawks | 1st | 52 | 42 | 957 | 5 | 2 | 0 | 24 | California |
2 | Nighthawks | 2nd | 25 | 2 | 1099 | 62 | 3 | 1 | 31 | Texas |
3 | Nighthawks | 2nd | 616 | 2 | 1400 | 26 | 3 | 1 | 2 | Florida |
4 | Dragoons | 1st | 43 | 4 | 1592 | 73 | 2 | 0 | 3 | Maine |
5 | Dragoons | 1st | 234 | 7 | 1006 | 37 | 1 | 1 | 4 | Iowa |
6 | Dragoons | 2nd | 523 | 8 | 987 | 949 | 2 | 0 | 24 | Alaska |
7 | Dragoons | 2nd | 62 | 3 | 849 | 48 | 3 | 1 | 31 | Washington |
8 | Scouts | 1st | 62 | 4 | 973 | 48 | 2 | 0 | 2 | Oregon |
9 | Scouts | 1st | 73 | 7 | 1005 | 435 | 1 | 0 | 3 | Wyoming |
10 | Scouts | 2nd | 37 | 8 | 1099 | 63 | 2 | 1 | 2 | Louisana |
11 | Scouts | 2nd | 35 | 9 | 1523 | 345 | 3 | 1 | 3 | Georgia |
Step 4. 'origin'칼럼을 인덱스로 설정후 저장해주세요¶
인덱스로 설정 후 army에 다시 대입해주셔야 합니다
In [14]:
army.set_index("origin", inplace=True)
army
Out[14]:
regiment | company | deaths | battles | size | veterans | readiness | armored | deserters | |
---|---|---|---|---|---|---|---|---|---|
origin | |||||||||
Arizona | Nighthawks | 1st | 523 | 5 | 1045 | 1 | 1 | 1 | 4 |
California | Nighthawks | 1st | 52 | 42 | 957 | 5 | 2 | 0 | 24 |
Texas | Nighthawks | 2nd | 25 | 2 | 1099 | 62 | 3 | 1 | 31 |
Florida | Nighthawks | 2nd | 616 | 2 | 1400 | 26 | 3 | 1 | 2 |
Maine | Dragoons | 1st | 43 | 4 | 1592 | 73 | 2 | 0 | 3 |
Iowa | Dragoons | 1st | 234 | 7 | 1006 | 37 | 1 | 1 | 4 |
Alaska | Dragoons | 2nd | 523 | 8 | 987 | 949 | 2 | 0 | 24 |
Washington | Dragoons | 2nd | 62 | 3 | 849 | 48 | 3 | 1 | 31 |
Oregon | Scouts | 1st | 62 | 4 | 973 | 48 | 2 | 0 | 2 |
Wyoming | Scouts | 1st | 73 | 7 | 1005 | 435 | 1 | 0 | 3 |
Louisana | Scouts | 2nd | 37 | 8 | 1099 | 63 | 2 | 1 | 2 |
Georgia | Scouts | 2nd | 35 | 9 | 1523 | 345 | 3 | 1 | 3 |
Step 5. veterans 칼럼만 조회해주세요¶
In [16]:
army["veterans"]
Out[16]:
origin
Arizona 1
California 5
Texas 62
Florida 26
Maine 73
Iowa 37
Alaska 949
Washington 48
Oregon 48
Wyoming 435
Louisana 63
Georgia 345
Name: veterans, dtype: int64
Step 6.'veterans'과 'deaths' 칼럼만 조회해주세요¶
In [17]:
army[["veterans","deaths"]]
Out[17]:
veterans | deaths | |
---|---|---|
origin | ||
Arizona | 1 | 523 |
California | 5 | 52 |
Texas | 62 | 25 |
Florida | 26 | 616 |
Maine | 73 | 43 |
Iowa | 37 | 234 |
Alaska | 949 | 523 |
Washington | 48 | 62 |
Oregon | 48 | 62 |
Wyoming | 435 | 73 |
Louisana | 63 | 37 |
Georgia | 345 | 35 |
Step 7. 열(column)의 이름들을 모두 조회해주세요¶
Index(['regiment', 'company', 'deaths', 'battles', 'size', 'veterans', 'readiness', 'armored', 'deserters'], dtype='object')
In [19]:
army.columns
Out[19]:
Index(['regiment', 'company', 'deaths', 'battles', 'size', 'veterans',
'readiness', 'armored', 'deserters'],
dtype='object')
Step 8. Maine과 Alaska의 'deaths', 'size', 'deserters'를 조회해주세요¶
In [21]:
army.loc[["Maine","Alaska"],['deaths', 'size', 'deserters']]
Out[21]:
deaths | size | deserters | |
---|---|---|---|
origin | |||
Maine | 43 | 1592 | 3 |
Alaska | 523 | 987 | 24 |
Step 9. 3행에서 7행까지, 그리고 3열에서 6열까지 조회해주세요¶
In [24]:
army.iloc[2:7,2:6]
Out[24]:
deaths | battles | size | veterans | |
---|---|---|---|---|
origin | ||||
Texas | 25 | 2 | 1099 | 62 |
Florida | 616 | 2 | 1400 | 26 |
Maine | 43 | 4 | 1592 | 73 |
Iowa | 234 | 7 | 1006 | 37 |
Alaska | 523 | 8 | 987 | 949 |
Step 10. 행은 4행부터, 열은 모든 열을 포함해서 조회해주세요¶
In [27]:
army.iloc[3:,:]
Out[27]:
regiment | company | deaths | battles | size | veterans | readiness | armored | deserters | |
---|---|---|---|---|---|---|---|---|---|
origin | |||||||||
Florida | Nighthawks | 2nd | 616 | 2 | 1400 | 26 | 3 | 1 | 2 |
Maine | Dragoons | 1st | 43 | 4 | 1592 | 73 | 2 | 0 | 3 |
Iowa | Dragoons | 1st | 234 | 7 | 1006 | 37 | 1 | 1 | 4 |
Alaska | Dragoons | 2nd | 523 | 8 | 987 | 949 | 2 | 0 | 24 |
Washington | Dragoons | 2nd | 62 | 3 | 849 | 48 | 3 | 1 | 31 |
Oregon | Scouts | 1st | 62 | 4 | 973 | 48 | 2 | 0 | 2 |
Wyoming | Scouts | 1st | 73 | 7 | 1005 | 435 | 1 | 0 | 3 |
Louisana | Scouts | 2nd | 37 | 8 | 1099 | 63 | 2 | 1 | 2 |
Georgia | Scouts | 2nd | 35 | 9 | 1523 | 345 | 3 | 1 | 3 |
Step 11. 행은 4행까지, 열은 모든 열을 포함해서 조회해주세요¶
In [29]:
army.iloc[:4,:]
Out[29]:
regiment | company | deaths | battles | size | veterans | readiness | armored | deserters | |
---|---|---|---|---|---|---|---|---|---|
origin | |||||||||
Arizona | Nighthawks | 1st | 523 | 5 | 1045 | 1 | 1 | 1 | 4 |
California | Nighthawks | 1st | 52 | 42 | 957 | 5 | 2 | 0 | 24 |
Texas | Nighthawks | 2nd | 25 | 2 | 1099 | 62 | 3 | 1 | 31 |
Florida | Nighthawks | 2nd | 616 | 2 | 1400 | 26 | 3 | 1 | 2 |
Step 12. 행은 모든 행을 포함하고, 열은 3열부터 7열까지 조회해주세요¶
In [30]:
army.iloc[:,2:7]
Out[30]:
deaths | battles | size | veterans | readiness | |
---|---|---|---|---|---|
origin | |||||
Arizona | 523 | 5 | 1045 | 1 | 1 |
California | 52 | 42 | 957 | 5 | 2 |
Texas | 25 | 2 | 1099 | 62 | 3 |
Florida | 616 | 2 | 1400 | 26 | 3 |
Maine | 43 | 4 | 1592 | 73 | 2 |
Iowa | 234 | 7 | 1006 | 37 | 1 |
Alaska | 523 | 8 | 987 | 949 | 2 |
Washington | 62 | 3 | 849 | 48 | 3 |
Oregon | 62 | 4 | 973 | 48 | 2 |
Wyoming | 73 | 7 | 1005 | 435 | 1 |
Louisana | 37 | 8 | 1099 | 63 | 2 |
Georgia | 35 | 9 | 1523 | 345 | 3 |
Step 13. Texas와 Arizona의 데이터를 조회해주세요¶
In [31]:
army.loc[["Texas","Arizona"]]
Out[31]:
regiment | company | deaths | battles | size | veterans | readiness | armored | deserters | |
---|---|---|---|---|---|---|---|---|---|
origin | |||||||||
Texas | Nighthawks | 2nd | 25 | 2 | 1099 | 62 | 3 | 1 | 31 |
Arizona | Nighthawks | 1st | 523 | 5 | 1045 | 1 | 1 | 1 | 4 |
Step 14. Arizona행에서 세번째 칼럼에 기록되어있는 값을 구하세요¶
523
In [34]:
army.iloc[0,2]
Out[34]:
523
Step 15. deaths열의 세번째 기록되어 있는 값을 구하세요¶
25
In [35]:
army.iloc[2,2]
Out[35]:
25
🐼 Pandas에 대해 한걸음 더더 가까워진 느낌!
728x90
'😁 빅데이터 문제 풀기 & Study > - 이외 사이트 문제' 카테고리의 다른 글
[Pandas] Pandas 연습 문제 풀기 -4 🐼 (0) | 2022.02.22 |
---|---|
[Pandas] Pandas 연습 문제 풀기 -3 🐼 (0) | 2022.02.22 |
[Pandas] Pandas 연습 문제 풀기 - 1 🐼 (0) | 2022.02.21 |
초보자를 위한 파이썬 300제 (241~290번) 10. 파이썬 모듈/11. 파이썬클래스 (0) | 2022.01.10 |
초보자를 위한 파이썬 300제 (290~300번) 12. 파일 입출력과 예외처리 (0) | 2022.01.07 |