RL Researcher

05. 판다스(Pandas) - Series 값 변경, Slicing 본문

AI Basic/Pandas

05. 판다스(Pandas) - Series 값 변경, Slicing

Lass_os 2021. 1. 4. 00:01

1. Series 값 변경


  • 추가 및 업데이트: 인덱스를 이용

  • 삭제: drop함수 이용

s = pd.Series(np.arange(100, 105), ['a', 'b', 'c', 'd', 'e'])
s

========================================================================

<output>
a    100
b    101
c    102
d    103
e    104
dtype: int64
s['a'] = 200
s

========================================================================

<output>
a    200
b    101
c    102
d    103
e    104
dtype: int64


s['k'] = 300
s

========================================================================

<output>
a    200
b    101
c    102
d    103
e    104
k    300
dtype: int64
s.drop('k', inplace=True)	# inplace = True : 원본에다 변경을 하겠다는 뜻
s

========================================================================

<output>
a    200
b    101
c    102
d    103
e    104
dtype: int64
s[['a', 'b']] = [300, 900]
s

========================================================================

<output>
a    300
b    900
c    102
d    103
e    104
dtype: int64

2. Series Slicing


  • 리스트, ndarray와 동일하게 적용

s1 = pd.Series(np.arange(100, 105))
s1

========================================================================

<output>
0    100
1    101
2    102
3    103
4    104
dtype: int64


s2 = pd.Series(np.arange(100, 105), ['a', 'c', 'b', 'd', 'e'])
s2

========================================================================

<output>
a    100
c    101
b    102
d    103
e    104
dtype: int64
s1[1:3]

========================================================================

<output>
1    101
2    102
dtype: int64


s2[1:3]

========================================================================

<output>
c    101
b    102
dtype: int64
s2['c':'d']

========================================================================

<output>
c    101
b    102
d    103
dtype: int64
Comments