59doit
[Python] indexing 본문
반응형
numpy 는 정수여야만 했지만 판다스는 정수외에 실수도 사용가능
▷
obj = pd.Series(np.arange(4.), index=['a', 'b', 'c', 'd'])
obj
# <출력>
# a 0.0
# b 1.0
# c 2.0
# d 3.0
# dtype: float64
obj['b']
# <출력> 1.0
obj[1]
# <출력> 1.0
obj[2:4]
# <출력>
# c 2.0
# d 3.0
# dtype: float64
obj[['b', 'a', 'd']]
# <출력>
# b 1.0
# a 0.0
# d 3.0
# dtype: float64
obj[[1, 3]]
# <출력>
# b 1.0
# d 3.0
# dtype: float64
obj[obj < 2]
# <출력>
# a 0.0
# b 1.0
# dtype: float64
obj['b':'c']
# <출력>
# b 1.0
# c 2.0
# dtype: float64
- 값 대체
obj['b':'c'] =5 # b,c 값을 5로 대체하라
obj
# <출력>
# a 0.0
# b 5.0
# c 5.0
# d 3.0
# dtype: float64
▷
하나 이상의 컬럼값 가져오기
data = pd.DataFrame(np.arange(16).reshape((4,4)),
index =['Ohio','Colorado','Utah','New York'],columns=['one','two','three','four'])
data
# <출력>
# one two three four
# Ohio 0 1 2 3
# Colorado 4 5 6 7
# Utah 8 9 10 11
# New York 12 13 14 15
data['two']
# <출력>
# Ohio 1
# Colorado 5
# Utah 9
# New York 13
# Name: two, dtype: int32
- Error
data['three','one']
# <출력> KeyError: ('three', 'one')
▼ 옳은것
data[['three','one']]
# <출력>
# three one
# Ohio 2 0
# Colorado 6 4
# Utah 10 8
# New York 14 12
▷
슬라이싱, 불리언 배열로 로우 선택가능
# <data>
# one two three four
# Ohio 0 1 2 3
# Colorado 4 5 6 7
# Utah 8 9 10 11
# New York 12 13 14 15
- 로우선택
data[:2]
# <출력>
# one two three four
# Ohio 0 1 2 3
# Colorado 4 5 6 7
- data 안의 data가 three 가 5 이상인 로우 값을 모두 가져오기
data[data['three']>5]
# <출력>
# one two three four
# Colorado 4 5 6 7
# Utah 8 9 10 11
# New York 12 13 14 15
- true false 값 추출하기
data<5
# <출력>
# one two three four
# Ohio True True True True
# Colorado True False False False
# Utah False False False False
# New York False False False Fals
- 데이터 값이 5이하인 데이터는 0으로 대체
data[data<5]=0
data
# <출력>
# one two three four
# Ohio 0 0 0 0
# Colorado 0 5 6 7
# Utah 8 9 10 11
# New York 12 13 14 15
▷
loc & iloc
# <data>
# one two three four
# Ohio 0 0 0 0
# Colorado 0 5 6 7
# Utah 8 9 10 11
# New York 12 13 14 15
- loc
data.loc['Colorado',['two','three']]
# <출력>
# two 5
# three 6
# Name: Colorado, dtype: int32
- iloc
data.iloc[2,[3,0,1]]
# <출력>
# four 11
# one 8
# two 9
# Name: Utah, dtype: int32
data.iloc[2]
# <출력>
# one 8
# two 9
# three 10
# four 11
# Name: Utah, dtype: int32
data.iloc[[1,2],[3,0,1]]
# <출력>
# four one two
# Colorado 7 0 5
# Utah 11 8 9
▷
문자쓸땐 문자까지 포함
data.loc[:'Utah','two']
# <출력>
# Ohio 0
# Colorado 5
# Utah 9
# Name: two, dtype: int32
utah까지
▷
숫자쓰면 숫자 전까지
data.iloc[:,:3][data.three >5]
# <출력>
# one two three
# Colorado 0 5 6
# Utah 8 9 10
# New York 12 13 14
0부터2 까지, three가 5이상인 애들 ; data.three!!!
반응형
'Programming > Python(파이썬)' 카테고리의 다른 글
[Python-Pandas] 수학메서드 (0) | 2022.11.09 |
---|---|
[Pandas] 넘파이에 의한 랜덤난수 (0) | 2022.11.08 |
[Pandas] arr (0) | 2022.11.08 |
[Pandas] #4 ser (0) | 2022.11.08 |
[Python-Pandas] #3 drop (0) | 2022.11.07 |
Comments