반응형
반응형

pandas

판다스(Pandas) 란?

  • 데이터 처리와 분석을 위한 파이썬 라이브러리
  • 파이썬의 엑셀이라고 보면된다.
  • 대표적으로 Series 와 DataFrame 클래스가 있다.
  • http://pandas.pydata.org

Series(시리즈)

  • Numpy는 데이터형이 하나만 지정될수 있지만 pandas는 혼합형이다.
  • 리스트와 달리 인덱스 이름을 부여할 수 있다.
  • 1차원 데이터만 다룸.
s1 = pd.Series(np.arange(20,25),index=['1번','2번','3번','4번','5번'])
s1
  • 출력
    1번 20
    2번 21
    3번 22
    4번 23
    5번 24
    dtype: int32
s1.index, type(s1.index), list(s1.index) ,s1['3번']
  • 출력
    (Index(['1번', '2번', '3번', '4번', '5번'], dtype='object'),
    pandas.core.indexes.base.Index,
    ['1번', '2번', '3번', '4번', '5번'],
    22)

데이타프레임이란?

  • 데이타를 표의 형태로 처리하는 자료구조이다.
s1 = ({'name':'홍길동','age':'24','gender':'male','mobile':'010-1234-4421'})
s2 = ({'name':'가길동','age':'42','gender':'female','mobile':'010-1122-3215'})
s3 = ({'name':'나길동','age':'51','gender':'male','mobile':'010-4321-2351'})
df =  pd.DataFrame([s1,s2,s3])
df
  • 출력
      name age gender mobile
    0 홍길동 24 male 010-1234-4421
    1 가길동 42 female 010-1122-3215
    2 나길동 51 male 010-4321-2351
df = pd.DataFrame( np.arange(1,26).reshape(5,5), 
                   index=[ 'row'+str(i) for i in range(1,6)], 
                   columns=[ 'col'+str(i) for i in range(1,6) ])
df
  • 출력
      col1 col2 col3 col4 col5
    row1 1 2 3 4 5
    row2 6 7 8 9 10
    row3 11 12 13 14 15
    row4 16 17 18 19 20
    row5 21 22 23 24 25

데이터프레임 속성

  • DataFrame변수.columns
  • DataFrame변수.index
  • DataFrame변수.values
  • dtype, shape, size
df = pd.DataFrame( np.arange(1,26).reshape(5,5), 
                   index=[ 'row'+str(i) for i in range(1,6)], 
                   columns=[ 'col'+str(i) for i in range(1,6) ])

print(type(df), df.shape, df.size)
print(df.index , df.columns)
df.values
  • 출력

<class 'pandas.core.frame.DataFrame'> (5, 5) 25
Index(['row1', 'row2', 'row3', 'row4', 'row5'], dtype='object') Index(['col1', 'col2', 'col3', 'col4', 'col5'], dtype='object')
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]])

 

 

 

반응형

+ Recent posts