머신러닝

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


머신러닝
머신러닝

2. DataFrame

페이지 정보

작성자 관리자 댓글 0건 조회 1,250회 작성일 20-01-22 18:28

본문

2. DataFrame

DataFrame은 2차원리스트(2차원배열) 같은 자료구조
R언어 data.frame 과 비슷하다. 

 

실습1.

import pandas as pd
from pandas import Series, DataFrame
import numpy as np

a = pd.DataFrame([
  [1,2,3],
  [4,5,6],
  [7,8,9]
])
 
print(a) 

 

[결과]

   0  1  2
0  1  2  3
1  4  5  6
2  7  8  9

 

 

실습2.

import pandas as pd
from pandas import Series, DataFrame
import numpy as np

data ={
  'city':['서울', '부산', '광주', '대구'],
  'year':[2000, 2001, 2002, 2002],
  'pop': [4000, 2000, 1000, 1000]
}

df = pd.DataFrame(data)

print(df)

 

[결과]

  city  year   pop
0   서울  2000  4000
1   부산  2001  2000
2   광주  2002  1000
3   대구  2002  1000

 

실습3.

import pandas as pd
from pandas import Series, DataFrame
import numpy as np

data ={
  'city':['서울', '부산', '광주', '대구'],
  'year':[2000, 2001, 2002, 2002],
  'pop': [4000, 2000, 1000, 1000]
}

df = DataFrame(data, columns=['year', 'city', 'pop'])
print(df)

df2 = DataFrame(data, columns=['year', 'city', 'pop', 'debt'], index=['one', 'two', 'three', 'four'])
print(df2)

 

[결과]

   year city   pop
0  2000   서울  4000
1  2001   부산  2000
2  2002   광주  1000
3  2002   대구  1000
       year city   pop debt
one    2000   서울  4000  NaN
two    2001   부산  2000  NaN
three  2002   광주  1000  NaN
four   2002   대구  1000  NaN

 

 

실습4.

import pandas as pd
from pandas import Series, DataFrame
import numpy as np

data ={
  'city':['서울', '부산', '광주', '대구'],
  'year':[2000, 2001, 2002, 2002],
  'pop': [4000, 2000, 1000, 1000]
}

df2 = DataFrame(data, columns=['year', 'city', 'pop', 'debt'], index=['one', 'two', 'three', 'four'])
print(df2)


print(df2['city'])
print(df2['year'])

print(df2.columns)

print(df2.index)

# iloc / loc : 로우(행)의 위치 접근할 때 사용하는 메소드
# loc는 색인을 name속성의 값으로 할당한다.​

# iloc는 색인을 숫자(0 ~ n-1)로 사용한다.

print(df2.loc['three'])

print(df2.iloc[2])  

 

[결과]

       year city   pop debt
one    2000   서울  4000  NaN
two    2001   부산  2000  NaN
three  2002   광주  1000  NaN
four   2002   대구  1000  NaN
one      서울
two      부산
three    광주
four     대구
Name: city, dtype: object
one      2000
two      2001
three    2002
four     2002
Name: year, dtype: int64
Index(['year', 'city', 'pop', 'debt'], dtype='object')
Index(['one', 'two', 'three', 'four'], dtype='object')
year    2002
city      광주
pop     1000
debt     NaN
Name: three, dtype: object
year    2002
city      광주
pop     1000
debt     NaN
Name: three, dtype: object

실습5.

​import pandas as pd
from pandas import Series, DataFrame
import numpy as np

data ={
  'city':['서울', '부산', '광주', '대구'],
  'year':[2000, 2001, 2002, 2002],
  'pop': [4000, 2000, 1000, 1000]
}

df2 = DataFrame(data, columns=['year', 'city', 'pop', 'debt'], index=['one', 'two', 'three', 'four'])
print(df2)

df2['debt'] = 1000

print(df2)


df2['debt'] = np.arange(4)

 


print(df2)

val =Series([1000, 2000, 3000, 4000] , index=['one', 'two', 'three', 'four'])

df2['debt'] = val

print(df2)

val1 = Series([1000, 3000, 5000], index = ['one', 'three', 'four'])
df2['debt'] = val1
print(df2)

 

 

[결과]

       year city   pop debt
one    2000   서울  4000  NaN
two    2001   부산  2000  NaN
three  2002   광주  1000  NaN
four   2002   대구  1000  NaN
       year city   pop  debt
one    2000   서울  4000  1000
two    2001   부산  2000  1000
three  2002   광주  1000  1000
four   2002   대구  1000  1000
       year city   pop  debt
one    2000   서울  4000     0
two    2001   부산  2000     1
three  2002   광주  1000     2
four   2002   대구  1000     3
       year city   pop  debt
one    2000   서울  4000  1000
two    2001   부산  2000  2000
three  2002   광주  1000  3000
four   2002   대구  1000  4000
       year city   pop    debt
one    2000   서울  4000  1000.0
two    2001   부산  2000     NaN
three  2002   광주  1000  3000.0
four   2002   대구  1000  5000.0

 

 

실습6.

import pandas as pd
from pandas import Series, DataFrame

data ={
  'city':['서울', '부산', '광주', '대구'],
  'year':[2000, 2001, 2002, 2002],
  'pop': [4000, 2000, 1000, 1000]
}

df2 = DataFrame(data, columns=['year', 'city', 'pop', 'debt'], index=['one', 'two', 'three', 'four'])
print(df2)

df2['aaa'] = df2.city =='서울'

print(df2)

del df2['aaa']

print(df2.columns)
print(df2)

 

[결과]

       year city   pop debt
one    2000   서울  4000  NaN
two    2001   부산  2000  NaN
three  2002   광주  1000  NaN
four   2002   대구  1000  NaN
       year city   pop debt    aaa
one    2000   서울  4000  NaN   True
two    2001   부산  2000  NaN  False
three  2002   광주  1000  NaN  False
four   2002   대구  1000  NaN  False
Index(['year', 'city', 'pop', 'debt'], dtype='object')
       year city   pop debt
one    2000   서울  4000  NaN
two    2001   부산  2000  NaN
three  2002   광주  1000  NaN
four   2002   대구  1000  NaN

 

 

 

실습7.

import pandas as pd
from pandas import Series, DataFrame

data2 = { 'seoul' : {2001: 20, 2002: 30},
  'busan':{2000: 10, 2001:200, 2002:300}
}


df3 = DataFrame(data2)
print(df3)

# df3.T 명령으로 행과 열을 바꿀 수 있다.​

print(df3.T)

#데이터프레임에서 values속성은 저장된 데이터를 2차원배열로 리턴한다. ​

print(df3.values)

 

[결과]

      seoul  busan
2001   20.0    200
2002   30.0    300
2000    NaN     10
        2001   2002  2000
seoul   20.0   30.0   NaN
busan  200.0  300.0  10.0
[[ 20. 200.]
 [ 30. 300.]
 [ nan  10.]]

 

 

댓글목록

등록된 댓글이 없습니다.


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

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

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