머신러닝

본문 바로가기
사이트 내 전체검색


머신러닝
머신러닝

1. Pandas 소개

페이지 정보

작성자 관리자 댓글 0건 조회 1,220회 작성일 20-01-22 10:17

본문

1. Pandas 소개

Pandas : 데이터 분석 기능을 제공하는 라이브러리,
예를 들면, CSV파일 등의 데이터를 읽고 원하는 데이터 형식으로 변환해줌


pandas 자료구조 : Series, DataFrame

Series는 일차원 배열 같은 자료구조

실습1.

​import pandas as pd
from pandas import Series, DataFrame

obj = Series([3, 22, 34, 11])

print(obj)

​print(obj.values)

print(obj.index)

[결과]

0     3
1    22
2    34
3    11
dtype: int64​

[ 3 22 34 11]
RangeIndex(start=0, stop=4, step=1)​

​실습2.

​import pandas as pd
from pandas import Series, DataFrame

obj2 = Series([4,5,6,2], index=['d', 'c', 'e', 'f'])

print(obj2)

print(obj2['c'])

print(obj2[['d', 'f', 'c']])

print(obj2*2)

print('b' in obj2)

print('c' in obj2)

[결과]​

d    4
c    5
e    6
f    2
dtype: int64
5
d    4
f    2
c    5
dtype: int64
d     8
c    10
e    12
f     4
dtype: int64
False
True

​실습3.

​import pandas as pd
from pandas import Series, DataFrame

data = {'kim':3400, 'hong':2000, 'kang':1000, 'lee':2400}

obj3 = Series(data)

print(obj3)

[결과]​

kim     3400
hong    2000
kang    1000
lee     2400
dtype: int64​

​실습4.

import pandas as pd
from pandas import Series, DataFrame

data = {'kim':3400, 'hong':2000, 'kang':1000, 'lee':2400}
name = ['woo', 'hong', 'kang', 'lee']

obj4 = Series(data, index=name)

print(obj4)

# 누락된 데이터를 찾을 때 사용하는 함수 isnull, notnull

print(pd.isnull(obj4))

print(pd.notnull(obj4))

[결과]

​woo        NaN
hong    2000.0
kang    1000.0
lee     2400.0
dtype: float64
woo      True
hong    False
kang    False
lee     False
dtype: bool
woo     False
hong     True
kang     True
lee      True
dtype: bool

실습5.

​import pandas as pd
from pandas import Series, DataFrame

data={"Seoul":4000, "Busan":2000, "Incheon":1500, "Kwangju":1000}

obj = Series(data)

print(obj)

cities = ["Seoul", "Daegu", "Incheon", "Kwangju"]
obj2 =Series(data, index=cities)

print(obj2)

print(obj + obj2)

 

 

[결과]

Seoul      4000
Busan      2000
Incheon    1500
Kwangju    1000
dtype: int64
Seoul      4000.0
Daegu         NaN
Incheon    1500.0
Kwangju    1000.0
dtype: float64
Busan         NaN
Daegu         NaN
Incheon    3000.0
Kwangju    2000.0
Seoul      8000.0
dtype: float64 

 

Series 객체와 Series의 색인(index)은 모두 name라는 속성이 있다.

 

실습6.

import pandas as pd
from pandas import Series, DataFrame

data={"Seoul":4000, "Busan":2000, "Incheon":1500, "Kwangju":1000}

cities = ["Seoul", "Daegu", "Incheon", "Kwangju"]
obj2 =Series(data, index=cities)

print(obj2)

 

obj2.name="인구수"


print(obj2)

obj2.index.name="도시"

print(obj2)

obj2.index=["Daejeon", "Busan", "jaeju", "jeonju"]

print(obj2) 

 

[결과]

Seoul      4000.0
Daegu         NaN
Incheon    1500.0
Kwangju    1000.0
dtype: float64
Seoul      4000.0
Daegu         NaN
Incheon    1500.0
Kwangju    1000.0
Name: 인구수, dtype: float64
도시
Seoul      4000.0
Daegu         NaN
Incheon    1500.0
Kwangju    1000.0
Name: 인구수, dtype: float64
Daejeon    4000.0
Busan         NaN
jaeju      1500.0
jeonju     1000.0
Name: 인구수, dtype: float64

 

댓글목록

등록된 댓글이 없습니다.


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.