프로젝트/코드프레소 체험단

파이썬으로 배우는 데이터 분석: Pandas - Series

KimCookieYa 2022. 4. 2. 01:42

Pandas 시작하기

import pandas as pd
print(pd.__version__)

 

 

Series

 - 1차원 배열의 자료구조

 - 인덱스를 통한 데이터 접근 가능

 - 동일한 데이터 타입의 값을 저장할 수 있음

 

 

pd.Series(data)

pd.Series(data)

 - 리스트, 튜플, 딕셔너리 등의 파이썬 객체를 data 인자로 전달하여 생성

 - 인덱스를 지정하지 않는 경우, 정수 0부터 시작되는 인덱스가 생성됨

 

 

pd.Series(data, index, name)

 - index: Series 객체 생성 시, index 설정하기

 - name: Series 객체 생성 시, 이름 지정하기

 

 

pd.Series(data=[딕셔너리], name)

 - 파이썬 딕셔너리: {key1: value1, key2: value2, ..., keyn: valuen}

 

 

Series 의 주요 속성

 - .index : 개별 데이터를 고유하게 식별하는 일종의 key 값

 - .values : Series 객체 내 모든 데이터 값

 - .dtype : Series 객체에 저장된 데이터의 타입

 - .shape : Series 객체의 모양

 - .name : Series 객체(데이터)의 이름

 - .index.name : Series 객체의 인덱스(index) 의 이름

 

# Pandas 라이브러리 로딩
import pandas as pd

# 설치된 버전 출력
print(pd.__version__)

# 리스트 데이터로 Series 객체 생성하기
year = ['2019', '2020', '2021', '2022']
result = pd.Series(data=year)

print('\nType:', type(result))
print(result)

# Series 객체의 주요 속성
print('\nIndex: ', result.index)
print('Data: ', result.values)
print('DType: ', result.dtype)
print('Shape: ', result.shape)
print('\n')

# Series 객체의 이름 지정
result.name = 'Year'
result.index.name = 'No.'
print(result)
print('\n')

# 인덱스 추가하여 Series 객체 생성하기
year = ['2019', '2020', '2021', '2022']
idx = ['a', 'b', 'c', 'd']
result = pd.Series(data=year, index=idx, name='Year')
print(result)
print('\n')

# 딕셔너리 데이터로 Series 객체 생성하기
score = {'Kim':85, 'Han':89, 'Lee':99, 'Choi':70}
result = pd.Series(data=score, name='Score')
print(result)
1.3.3
Type: <class 'pandas.core.series.Series'>
0 2019
1 2020
2 2021
3 2022
dtype: object
Index: RangeIndex(start=0, stop=4, step=1)
Data: ['2019' '2020' '2021' '2022']
DType: object
Shape: (4,)
No.
0 2019
1 2020
2 2021
3 2022
Name: Year, dtype: object
a 2019
b 2020
c 2021
d 2022
Name: Year, dtype: object
Kim 85
Han 89
Lee 99
Choi 70
Name: Score, dtype: int64