Python/Pandas Tutorial

[Python] Pandas Tutorial :: pandas란? 데이터프레임이란? 시리즈란?

슈퍼짱짱 2020. 9. 18. 15:45
반응형

Pandas Tutorial :: Chapter 1

 

Pandas란 R에서 Data Frame과 같은 역할을 하며, 엑셀과 비슷한 역할을 한다고 보면 된다.

 

0. import pandas library

pandas library를 import 하고 편의를 위해 pd라 이름붙여 준다.  (padnas.~~ 이 아니라 pd.~~ 로 줄여서 불러올 수 있다.)

 

import pandas as pd

 

1. Read csv file with pandas

pandas로 로컬에 저장되어있는 csv 파일을 불러오는 코드는 다음과 같다.

data_frame 이라는 이름으로 저장해 주었다.

 

data_frame = pd.read_csv('01. Data/friend_list.csv')

 

본 과정에서 불러온 파일은 아래 첨부된 파일이다.

friend_list.csv
0.00MB

 

괄호 안에는 해당 파일이 저장되어있는 경로를 넣어주면 된다.

 

참고

 

전체 경로를 다 넣어주어도 되지만, 현재 스크립트를 저장하고 있는 경로가 자동으로 상대 경로로 지정되기 때문에 위와같이 넣어주어도 된다. 

아래 사진에서 현재 script는 padnasTutorial.py고, 데이터는 script와 같은 폴더에 있는 01. Data 폴더에 저장되어있다.

 

 

결과는 다음과 같다.

 

> data_frame

 

    name  age        job
0   John   20    student
1  Jenny   30  developer
2   Nate   30    teacher
3  Julia   40    dentist
4  Brian   45     managr
5  Chris   25     intern

 

2. head : 상위 n개 데이터 확인

R에서 head()와 같은 기능을한다. 상위 몇 개의 데이터를 확인할 수 있다. 확인하고 싶은 데이터 개수를 괄호안에 넣어주어도 되고, 안 넣어주면 알아서 몇개만 보여준다.

 

data_frame.head()
data_frame.head(2) ## 보고 싶은 데이터 개수 parameter로 전달

 

> data_frame.head(2)

 

    name  age        job
0   John   20    student
1  Jenny   30  developer

 

3. tail : 하위 n개 데이터 확인

마찬가지로 tail은 하위 데이터를 확인할 수 있다.

 

> data_frame.tail(2

   name  age     job
4  Brian   45  managr
5  Chris   25  intern

 

4. Series

Data Frame 을 구성하고 있는 각 컬럼의 타입은 Series 이다.

즉, Pandas 에서 데이터 프레임은 시리즈로 구성되어 있다.

 

data_frame 자체의 타입은 DataFrame 이고, 각 컬럼의 타입은 Series 이다.

 

> type(data_frame)

pandas.core.frame.DataFrame

 

> type(data_frame.name)

pandas.core.series.Series

 

> type(data_frame.age)

pandas.core.series.Series

 

> type(data_frame.job)

pandas.core.series.Series

 

5. Create Series

시리즈는 list 형태로 만들 수 있다.

 

s1 = pd.core.series.Series([1,2,3])
s2 = pd.core.series.Series(["one","two","three"])

 

> s1

 

0    1
1    2
2    3
dtype: int64

 

> type(s1)

pandas.core.series.Series

 

6. Create DataFrame 

위에서 만든 시리즈로 데이터프레임 만드는 코드는 다음과 같다.

s1은 num이라는 이름의 컬럼으로, s2는 word라는 컬럼으로 만들었다.

 

pd.DataFrame(data = dict(num = s1, word = s2))

 

   num   word
0    1    one
1    2    two
2    3  three

 


전체 코드

 

###################################################
# 제목  : Pandas Tutorial
# 작성자 : SK C&C 이다경 선임
# 작성일 : 20200918
#
# Description
#
#   [SK C&C] Pandas Tutorial
###################################################

# 0. import library
import pandas as pd

# 1. LOADING DATA (csv file saved local)
data_frame = pd.read_csv('01. Data/friend_list.csv')

data_frame
#     name  age        job
# 0   John   20    student
# 1  Jenny   30  developer
# 2   Nate   30    teacher
# 3  Julia   40    dentist
# 4  Brian   45     managr
# 5  Chris   25     intern

# 2. head : 상위 n 개 데이터 확인
data_frame.head()
data_frame.head(2) ## 보고 싶은 데이터 개수 parameter로 전달

# 3. tail : 하위 n 개 데이터 확인
data_frame.tail(2)

# 4. Series
## data frame을 구성하는 각 컬럼을 series type 임.
## # 즉, pandas 데이터 프레임은 시리즈로 구성되어 있음.

type(data_frame)
# pandas.core.frame.DataFrame

type(data_frame.name)
# pandas.core.series.Series
type(data_frame.age)
# pandas.core.series.Series
type(data_frame.job)
# pandas.core.series.Series

# 5. create series :: list
list_temp = [1,2,3]
list_temp
# Out[21]: [1, 2, 3]

s1 = pd.core.series.Series([1,2,3])
s2 = pd.core.series.Series(["one","two","three"])

s1
# 0    1
# 1    2
# 2    3
# dtype: int64

type(s1)
# pandas.core.series.Series

# 6. create dataframe
pd.DataFrame(data = dict(num = s1, word = s2))
#    num   word
# 0    1    one
# 1    2    two
# 2    3  three

 


출처

 

SK C&C Learning Portal [외부] Pandas Tutorial

www.youtube.com/watch?v=ZeeUbTsyKlQ&list=PLVNY1HnUlO25etNe08s1H1nIIbbZdJwxA

 

반응형