2 Sat

TIL

[인프런] 단 두 장의 문서로 데이터 분석과 시각화 뽀개기

일부 컬럼을 기준으로 데이터 가져오기 - Subset Variables (Columns)

import seaborn as sns
df = sns.load_dataset("iris")
df.head()

sepal_length

sepal_width

petal_length

petal_width

species

0

5.1

3.5

1.4

0.2

setosa

1

4.9

3.0

1.4

0.2

setosa

2

4.7

3.2

1.3

0.2

setosa

3

4.6

3.1

1.5

0.2

setosa

4

5.0

3.6

1.4

0.2

setosa

columns = ['sepal_length', 'sepal_width', 'species']
df[columns].head()

sepal_length

sepal_width

species

0

5.1

3.5

setosa

1

4.9

3.0

setosa

2

4.7

3.2

setosa

3

4.6

3.1

setosa

4

5.0

3.6

setosa

0

1

2

3

4

현재는 조건이 없어서 인덱스만 추출

0

1

2

3

4

'.' 이 들어간 column을 추출한다. 이 때는 '.'이 들어간 column이 없다. 또한 '.'은 정규식에서 다른 의미로도 쓰이기 때문에 \. 로 사용한다.

sepal_length

sepal_width

petal_length

petal_width

0

5.1

3.5

1.4

0.2

1

4.9

3.0

1.4

0.2

2

4.7

3.2

1.3

0.2

3

4.6

3.1

1.5

0.2

4

5.0

3.6

1.4

0.2

sepal_length

petal_length

0

5.1

1.4

1

4.9

1.4

2

4.7

1.3

3

4.6

1.5

4

5.0

1.4

$가 뒤에오면 특정 문자로 끝나는 결과만 추출한다.

sepal_length

sepal_width

0

5.1

3.5

1

4.9

3.0

2

4.7

3.2

3

4.6

3.1

4

5.0

3.6

^가 앞에오면 특정 문자로 시작하는 결과만 추출한다.

sepal_length

sepal_width

petal_length

petal_width

species

0

5.1

3.5

1.4

0.2

setosa

1

4.9

3.0

1.4

0.2

setosa

2

4.7

3.2

1.3

0.2

setosa

3

4.6

3.1

1.5

0.2

setosa

4

5.0

3.6

1.4

0.2

setosa

loc를 이용해서 범위 설정을 할 수 있다.

sepal_width

petal_length

petal_width

2

3.2

1.3

0.2

3

3.1

1.5

0.2

4

3.6

1.4

0.2

5

3.9

1.7

0.4

sepal_width

petal_length

species

145

3.0

5.2

virginica

146

2.5

5.0

virginica

147

3.0

5.2

virginica

148

3.4

5.4

virginica

149

3.0

5.1

virginica

Last updated

Was this helpful?