머신러닝

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


머신러닝
머신러닝

4. 색인, 선택

페이지 정보

작성자 관리자 댓글 0건 조회 1,293회 작성일 20-01-22 21:26

본문

4. 색인, 선택

삭제하기

 

실습1.

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

obj = Series(np.arange(5), index=['a','b','c','d','e'])

print(obj)

obj2=obj.drop('c')
print(obj2)

obj3=obj.drop(['b', 'd', 'c'])

print(obj3)

 

 

[결과]

a    0
b    1
c    2
d    3
e    4
dtype: int32
a    0
b    1
d    3
e    4
dtype: int32
a    0
e    4
dtype: int32

 

axis는 축을 나타낸다. 0은 row, 1은 column을 나타낸다.

 

실습2.

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

df = DataFrame(np.arange(16).reshape(4,4), index=['seoul', 'busan','daegu','incheon'], columns=['one', 'two', 'three', 'four'])


print(df)

new_df = df.drop(['seoul', 'busan'])

print(new_df)

new_df = df.drop('three', axis=1)
print(new_df)

new_df = df.drop(['two', 'four'], axis=1)
print(new_df) 

 

[결과]

         one  two  three  four
seoul      0    1      2     3
busan      4    5      6     7
daegu      8    9     10    11
incheon   12   13     14    15
         one  two  three  four
daegu      8    9     10    11
incheon   12   13     14    15
         one  two  four
seoul      0    1     3
busan      4    5     7
daegu      8    9    11
incheon   12   13    15
         one  three
seoul      0      2
busan      4      6
daegu      8     10
incheon   12     14

 

색인, 선택, 슬라이싱

 

실습3.

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

obj = Series(np.arange(4.), index=['a','b','c','d'])
print(obj)

print(obj['b':'c'])

obj['b':'c'] = 10

print(obj)

 

[결과]

a    0.0
b    1.0
c    2.0
d    3.0
dtype: float64
b    1.0
c    2.0
dtype: float64
a     0.0
b    10.0
c    10.0
d     3.0
dtype: float64

실습4.

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

data = DataFrame(np.arange(16).reshape(4,4),
            index=['seoul', 'busan', 'kwangju', 'daegu'],
            columns=['one', 'two', 'three', 'four'])

print(data)

print(data['two'])


print(data[['one', 'two']])

 

print(data[:2])

print(data[data['three'] > 7])

print(data < 5)

data[data<5] = 0

print(data)

 

 

[결과]

         one  two  three  four
seoul      0    1      2     3
busan      4    5      6     7
kwangju    8    9     10    11
daegu     12   13     14    15
seoul       1
busan       5
kwangju     9
daegu      13
Name: two, dtype: int32
         one  two
seoul      0    1
busan      4    5
kwangju    8    9
daegu     12   13
       one  two  three  four
seoul    0    1      2     3
busan    4    5      6     7
         one  two  three  four
kwangju    8    9     10    11
daegu     12   13     14    15
           one    two  three   four
seoul     True   True   True   True
busan     True  False  False  False
kwangju  False  False  False  False
daegu    False  False  False  False
         one  two  three  four
seoul      0    0      0     0
busan      0    5      6     7
kwangju    8    9     10    11
daegu     12   13     14    15

 

 

실습5.

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

data = DataFrame(np.arange(16).reshape(4,4),
            index=['seoul', 'busan', 'kwangju', 'daegu'],
            columns=['one', 'two', 'three', 'four'])

print(data)

# iloc / loc : DataFrame의 특수한 색인 필드(속성)​

print(data.iloc[2])
print(data.iloc[2, 3])

print(data.loc['seoul'])
print(data.loc['busan', ['two', 'three']])
print(data.loc[['kwangju', 'daegu'], ['three', 'four']])

print(data.loc[['seoul', 'daegu'], ['three', 'one']])

print(data.loc[:'kwangju', 'three'])

 

 

[결과]

         one  two  three  four
seoul      0    1      2     3
busan      4    5      6     7
kwangju    8    9     10    11
daegu     12   13     14    15
one       8
two       9
three    10
four     11
Name: kwangju, dtype: int32
11
one      0
two      1
three    2
four     3
Name: seoul, dtype: int32
two      5
three    6
Name: busan, dtype: int32
         three  four
kwangju     10    11
daegu       14    15
       three  one
seoul      2    0
daegu     14   12
seoul       2
busan       6
kwangju    10
Name: three, dtype: int32

 

 

댓글목록

등록된 댓글이 없습니다.


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

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

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