본문 바로가기
Python/Data Analysis

[판다스(Pandas)] 1. 시리즈(Series)

by 리미와감자 2023. 11. 13.

판다스(Pandas) 기초

 

판다스(Pandas)란?

판다스(Pandas)는 파이썬에서 대중적으로 사용하는 데이터 처리를 위한 라이브러리이다.


Pandas의 자료 구조

 

https://www.jiyik.com/w/pandas/pandas-dataframe

 

1. 시리즈(Series): 1차원 데이터 구조

2. 데이터프레임(Data Frame): 2차원 데이터 구조

3. 패널(Panel): 3차원 데이터 구조

 

여러 개의 시리즈가 모여 하나의 Data Frame이 될 수 있다.

 

주로 Data Frame이 많이 사용된다.


Pandas 라이브러리 설치

import pandas as pd

1. Series 클래스

1.1 Series 객체 생성

 

1월부터 4월까지 온도 : -20, -10, 10, 20

 

# 1. Series : 1차원 데이터
# 1.1 Series 객체 생성
temp = pd.Series([-20,-10,10,20])
print(temp)
print()
print(temp.values)
print()
print(temp.index)
print()
print(temp.dtypes)

 

 

0   -20
1   -10
2    10
3    20
dtype: int64

[-20 -10  10  20]

RangeIndex(start=0, stop=4, step=1)

int64


1.2 Series 객체 생성 (Index 지정)

 

# 1.2 Series 객체 생성 (Index 지정)
temp = pd.Series([-20,-10,10,20], index=['Jan','Feb','Mar','Apr'])
print(temp)
print()
print(temp.values)
print()
print(temp.index)
print()
print(temp.dtypes)

 

 

Jan   -20
Feb   -10
Mar    10
Apr    20
dtype: int64

[-20 -10  10  20]

Index(['Jan', 'Feb', 'Mar', 'Apr'], dtype='object')

int64

 


1.3 Series 객체 이름 지정

# 1.3 Series 객체 이름 지정
temp = pd.Series([-20,-10,10,20], index=['Jan','Feb','Mar','Apr'])
temp.name = 'Temperature'
temp.index.name = 'Month'
print(temp)
print()
print(temp.values)
print()
print(temp.index)
print()
print(temp.dtypes)

 

Month
Jan   -20
Feb   -10
Mar    10
Apr    20
Name: Temperature, dtype: int64

[-20 -10  10  20]

Index(['Jan', 'Feb', 'Mar', 'Apr'], dtype='object', name='Month')

int64

 


1.4 딕셔너리로 Series 객체 만들기

 

파이썬의 딕셔너리 자료형을 Series data로 만들 수 있다. 딕셔너리의 Key가 Series의 Index가 된다.

# 1.4 딕셔너리로 Series 객체 만들기
temp = {'Jan': -20, 'Feb': -10, 'Mar': 10, 'Apr': 20}
temp = pd.Series(temp)
temp.name = 'Temperature'
temp.index.name = 'Month'
print(temp)
print()
print(temp.values)
print()
print(temp.index)
print()
print(temp.dtypes)

 

Month
Jan   -20
Feb   -10
Mar    10
Apr    20
Name: Temperature, dtype: int64

[-20 -10  10  20]

Index(['Jan', 'Feb', 'Mar', 'Apr'], dtype='object', name='Month')

int64

댓글