본문 바로가기

코딩으로 익히는 Python/Pandas

[Python] 11. pandas DataFrame 인덱싱(Indexing), 슬라이싱(Slicing) 예제

728x90
반응형
SMALL
import pandas as pd
import numpy as np

 

data =  {'eng':[10,30,50,70],
         'kor':[20,40,60,80],
         'math':[90,50,20,70]}
df = pd.DataFrame(data, index=['a','b','c','d'] )
df


# df['a']
# df['컬럼명']
df['eng']
[OUT] :

a    10
b    30
c    50
d    70
Name: eng, dtype: int64

 

df[['eng','math']]

 

df['eng']['a':'c']
[OUT] :

a    10
b    30
c    50
Name: eng, dtype: int64

 

df[['eng']]

 

# df[슬라이싱(row)]
df[1:3]

 

df

 

df['b':'c']


iloc 인덱싱 슬라이싱(zero base index 기준)

# df.iloc[행,열]
df.iloc[0]
[OUT] :

eng     10
kor     20
math    90
Name: a, dtype: int64

 

df.iloc[0,0]
[OUT] :

10

 

df.iloc[[0,2,3]]

 

df.iloc[1:,1]
[OUT] :

b    40
c    60
d    80
Name: kor, dtype: int64

 

df.iloc[1:,1:]

 

df.iloc[1:3,[0,2]]


loc 인덱싱 슬라이싱(부여된 인덱스명 컬럼명)

# df.loc[행,컬럼]
df.loc['a']
[OUT] :

eng     10
kor     20
math    90
Name: a, dtype: int64

 

df.loc['a':'c']

 

df.loc[['a','c','d']]

 

df.loc['b':]

 

df.loc['b':,'kor':]

 

df.loc['b':'c',['eng','math']]

 

df


review
- df.loc[['a','c','d']] -> 원하는 행만 추출 하고 싶을 땐 리스트 사용
728x90
반응형
LIST