18_Merge

Merge

  • Merge Docs를 보면 데이터베이스의 Join 처럼 columns과 indexes을 사용하여 DataFrame을 Merge한다고 정의되어 있음
  • 기본 메소드 형태는 pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True,suffixes=('_x', '_y'), copy=True, indicator=False)
  • 중요 인자를 보면
    • left, right :Merge의 대상이 되는 DataFrame( Series면 DataFrame으로 변환해야 함)
    • how:기본은 'inner'로 되어 있고. left, right, outer를 선택할 수 있음
    • on :merge의 기준이 되는 key변수, left와 right에 동일 컬럼명 있을 경우 사용(Must be found in both DataFrames)
    • left_on, right_on: 왼쪽, 오른쪽 각각 merge의 기준이 되는 key변수. (label or list)
    • left_index, right_index:기본으로 False이며 True이면 Merege 키로 사
    • suffixes=('_x', '_y'):중복 컬럼시 접두어를 선택할 수 있음.
    • indicator = Merge된 DataFrame에 how에 따른 left_only, right_only, both의 출처를 표시.

Continue reading 18_Merge

17 Concat

Concat, Append

  • Concat Docs를 보면 Concat 메소드는 축의 방향에 따라 Pandas object(DataFrame, Series 등등)을 연결(합침)한다고 정의되어 있음 (Concatenate pandas objects along a particular axis with optional set logic along the other axes)
  • 기본 메소드 형태는 pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True)
  • 중요 인자를 보면

    • objs: DataFrame, Series
    • axis=0:기본은 0으로 되어 있어 상하로 합치고, 1로 선택하면 좌우로 합침
    • join='outer: 기본은 outer로 되어 있 합집합을 합치고, 'inner'를 선택하면 교집합을 합침
  • Append는 Concat과 비슷하지만 DataFrame에만 적용되고 오직 DataFrame의 세로축으로만 마지막에 추가되고 return 값으로 DataFrame을 반환하다.

    • Append의 인자로는 DataFrame, Series, dict-like object, list등이 사용 될 수 있다.

Continue reading 17 Concat

16 Sort

Sort

  • Pandas에서는 sort_values메소드를 사용하여 columns과 index(row) 별로 정렬할 수 있다. Sort_Values Docs

    • index(row)로 정렬 할 경우에는 sort_values에 추가적으로 axis = 1가 필요하다.
    • 컬럼이 2개인 인상인 경우에는 다음과 형태로 표현한다. df.sort(['A', 'B'], ascending=[1, 0])
  • sort_index를 사용하여 index의 label을 정렬할 수 있다.Sort_Index

    • 축이 세로 방향인 index level을 정렬할 경우에는 axis가 필요없지만 축이 가로 방향인 column level 을 정렬할 경우에는 axis = 0가 필요하다.

Continue reading 16 Sort

15 MultiIndex

MultiIndex

  • MultiIndex는 A multi-level, or hierarchical, index object로 정의된다.MultiIndex Docs
  • MultiIndex 중요 세요소로는
    • levels : 계층에 대한 이름(The unique labels for each level)
    • labels : 각 이름별 계층 위치 (Integers for each level designating which label at each location)
    • names : 레벨에 대한 이름( Names for each of the index levels.)
  • MultiIndex 생성하는 메소드는 다음과 같음
    • from_arrays(arrays[, sortorder, names])
    • from_tuples(tuples[, sortorder, names])
    • from_product(iterables[, sortorder, names])
  • unstack()을 사용하여 columns을 index의 변경하여 MultiIndex를 생성할 수 있음 .

Continue reading 15 MultiIndex

14 Index, Column Method

Index, Column조작

  • Pandas에서는 Index와 Columns이 객체이므로 객체 안에 중요한 정보를 다루기 위한 메소드들이 존재함
  • 대표적인 방법들 정리해 보면
    1. reset_index: index를 columns의 하나로 변형시키고 0부터 n-1 까지 새로운 index를 추가
    2. set_index: reset_index와는 반대로 columns 중에 하나를 index로 변경
    3. drop([row]) : index의 row를 삭제함
    4. drop([col], axis=1): 컬럼을 삭제
    5. df[df.name != 'Tina']: 특정컬럼의 조건 로우만 삭제
    6. 컬럼이름 변경: df.rename
    7. 컬럼 Slicing을 통한 DataFrame내 컬럼 내 위치변경

Continue reading 14 Index, Column Method

Pagination


© 2017. All rights reserved.

Powered by ZooFighter v0.12