RL Researcher

01. 판다스(Pandas) - Index, value 활용 본문

AI Basic/Pandas

01. 판다스(Pandas) - Index, value 활용

Lass_os 2021. 1. 3. 23:03

- 판다스 공식링크(pandas.pydata.org/)

- Document(pandas.pydata.org/docs/)

1. 판다스 모듈 불러오기


import의 사전적 의미는 '다른 컴퓨터 시스템으로부터 자기의 시스템 안에 데이터 등을 들여놓는 행위'라고 정의하고 있습니다.

import는 이미 만들어진 파이썬 프로그램 파일, 라이브러리안에 있는 파일 등을 사용할 수 있게 해주는 명령어입니다. 여기서 라이브러리는 도서관이라는 뜻이며, 파이썬 에서 사용가능한 유용한 프로그램들을 모아놓은 곳입니다. 기본적인 라이브러리는 파이썬 설치 시 자동으로 컴퓨터에 설치가 됩니다.

# numpy package도 사용하기 위해 numpy도 import
import numpy as np
import pandas as pd

2. 시리즈(Series)


  • pandas의 기본 객체 중 하나

  • numpy의 ndarray를 기반으로 인덱싱을 기능을 추가하여 1차원 배열을 나타냄

  • index를 지정하지 않을 시, 기본적으로 ndarray와 같이 0-based 인덱스 생성, 지정할 경우 명시적으로 지정된 index를 사용

  • 같은 타입의 0개 이상의 데이터를 가질 수 있음

s1 = pd.Series([1, 2, 3])
s1

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

<output>
0    1
1    2
2    3
dtype: int64


s2 = pd.Series(['a', 'b', 'c'])
s2

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

<output>
0    a
1    b
2    c
dtype: object


s3 = pd.Series(np.arange(200))
s3

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

<output>
0        0
1        1
2        2
3        3
4        4
      ... 
195    195
196    196
197    197
198    198
199    199
Length: 200, dtype: int64
  • data, index함께 명시하기

s4 = pd.Series([1, 2, 3], [100, 200, 300])
s4

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

<output>
100    1
200    2
300    3
dtype: int64


s5 = pd.Series([1, 2, 3], ['a', 'm', 'k'])
s5

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

<output>
a    1
m    2
k    3
dtype: int64


s6 = pd.Series(np.arange(5), np.arange(100, 105), dtype=np.int16)
s6

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

<output>
100    0
101    1
102    2
103    3
104    4
dtype: int16
  • 리스트 활용하기

s6.index

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

<output>
Int64Index([100, 101, 102, 103, 104], dtype='int64')


s6.values

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

<output>
array([0, 1, 2, 3, 4], dtype=int16)
  • 인덱스를 통한 데이터 접근

s6[104]

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

<output>
4


s6[103]

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

<output>
3
  • 인덱스를 통한 데이터 업데이트

s6[104] = 70
s6

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

<output>
100     0
101     1
102     2
103     3
104    70
dtype: int16


s6[105] = 90
s6[200] = 80
s6

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

<output>
100     0
101     1
102     2
103     3
104    70
105    90
200    80
dtype: int64
  • 인덱스 재사용하기

s7 = pd.Series(np.arange(7), s6.index)
s7

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

<output>
100    0
101    1
102    2
103    3
104    4
105    5
200    6
dtype: int64
Comments