DataFrame에서 컬럼명을 수정하는 방법은 몇 개가 존재한다.

아래와 같은 DataFrame 샘플이 존재한다고 할 때

import pandas as pd
df = pd.DataFrame( {'c1':[1, 2, 3]
                   ,'c2':[3, 4, 5]
                   ,'c3':[6, 7, 8]})

	c1	c2	c3
0	1	3	6
1	2	4	7
2	3	5	8

 

□ 특정 컬럼명 수정

아래 세 가지 방법으로 컬럼명 수정이 가능하다.

df.rename( {'c1':'A1'}, axis='columns', inplace=True)
# 이때 inplace=False이면 원본 데이터(df) 값 자체는 변경하지 않는다. default는 False이다.

	A1	c2	c3
0	1	3	6
1	2	4	7
2	3	5	8


df.rename( columns={'c1':'B1'}, inplace=True)  

	B1	c2	c3
0	1	3	6
1	2	4	7
2	3	5	8


df.rename( columns={df.columns[0]:'C1'}, inplace=True)

	C1	c2	c3
0	1	3	6
1	2	4	7
2	3	5	8


df.columns.values[0] = 'D1'

	D1	c2	c3
0	1	3	6
1	2	4	7
2	3	5	8

- inplace option

DataFrame에서 inplace option은 원본 데이터(source) 자체를 변경하는가, 하지 않는가를 지정한다.

inplace=False인 경우에는 원본 데이터 자체는 변경하지 않고 형상만 변경한다. 이때 해당 형상은 return 값을 통해서 받을 수 있다.

 

temp = df.rename( {'c1', 'A1'}, axis='columns', inplace=False) 

 

의 경우 원본 데이터인 df의 첫 번째 컬럼명은 A1으로 변경되지 않는다.

반면 temp는 컬럼명이 A1으로 변경된 데이터를 가지게 된다. 

 

같은 원리로

df.rename( {'c1', 'A1'}, axis='columns', inplace=True) 은

df = df.rename( {'c1', 'A1'}, axis='columns', inplace=False)  과 동일한 결과를 가진다.

 

 

□ 컬럼명 전체 변경

아래 방법으로 컬럼명 전체 수정이 가능하다.

df.rename( {'c1':'A1', 'c2':'A2', 'c3':'A3'}, axis='columns', inplace=True)

	A1	A2	A3
0	1	3	6
1	2	4	7
2	3	5	8


df.rename( columns={'c1':'B1', 'c2':'B2', 'c3':'B3'}, inplace=True)

	B1	B2	B3
0	1	3	6
1	2	4	7
2	3	5	8


df.columns = ['C1', 'C2', 'C3']

	C1	C2	C3
0	1	3	6
1	2	4	7
2	3	5	8

 

반응형

□ DataFrame.loc 사용법

구분

기본 사용법

다른 형태

row name

df.loc[ "row1" ]

 

row name list

df.loc[ "row1", "row2", "row3" ]

 

row name list slicing

df.loc[ "row1" : "row5" ]

df[ "row1" : "row5" ]

column name

df.loc[ :, "col1" ]

df[ "col1" ]

column name list

df.loc[ :, [ "col1", "col2", "col3" ] ]

df[ [ "col1", "col2", "col3" ] ]

column name list slicing

df.loc[ :, "col1" : "col5" ]

 

 

□ DataFrame.iloc 사용법

구분

기본 사용법

다른 형태

row

df.iloc[ 1 ]

 

row list

df.iloc[ [ 1, 2, 3 ] ]

 

row list slicing

df.iloc[ 1 : 5 ]

df[ 1 : 5 ]

column

df.iloc[ :, 1 ]

 

column list

df.iloc[ :, [ 1, 2, 3 ] ]

 

column list slicing

df.iloc[ :, 1 : 5 ]

 

 

반응형


파이썬에서 Import 방법은 두 가지가 있습니다.


import 모듈

→ 해당 모듈 전체를 가져온다.

    사용하려면 항상 '모듈명.메소드' 와 같이 모듈명을 앞에 붙여주어야 한다.


from 모듈 import 메소드 / 변수

→ 해당 모듈 내에 있는 특정 메소드나 모듈 내 정의된 변수를 가져온다.

    가져온 메소드나 변수를 앞에 모듈명을 붙이지 않고 그대로 사용할 수 있다.

    다만, 이름이 같은 변수나 메소드가 존재할 경우 대체된다.

    

    from 모듈 import * 이라고 하면 import 모듈과 동일하다. (사용 시 모듈명 붙이는 것 빼고)


아래는 두 방식에 대해 잘 설명된 사이트입니다. 단, 파이썬 2.7 버전을 중심으로 설명하고 있습니다.


□ 원본 URL → https://wikidocs.net/77



반응형

 

□ Jupyter notebook 명령키 일람

 

Command Mode (press Esc to enable) Edit Mode (press Enter to enable)
Enter   enter edit mode Tab   code completion or indent
Shift-Enter   run cell, select below Shift-Tab   tooltip
Ctrl-Enter   run cell Ctrl-]   indent
Alt-Enter   run cell, insert below Ctrl-[   dedent
Y   to code Ctrl-A   select all
M   to markdown Ctrl-Z   undo
R   to raw Ctrl-Shift-Z   redo
1  to heading 1 Ctrl-Y   redo
2  to heading 2 Ctrl-Home   go to cell start
3  to heading 3 Ctrl-Up   go to cell start
4  to heading 4 Ctrl-End   go to cell end
5  to heading 5 Ctrl-Down   go to cell end
6  to heading 6 Ctrl-Left   go one word left
Up   select cell above Ctrl-Right   go one word right
K   select cell above Ctrl-Backspace   delete word before
Down   select cell below Ctrl-Delete   delete word after
J   select cell below Esc   command mode
A   insert cell above Ctrl-M   command mode
B   insert cell below Shift-Enter   run cell, select below
X   cut selected cell Ctrl-Enter   run cell
C   copy selected cell Alt-Enter   run cell, insert below
Shift-V   paste cell above Ctrl-Shift-Subtract   split cell
V   paste cell below Ctrl-Shift--   split cell
Z   undo last cell deletion Ctrl-S   Save and Checkpoint
D,D   delete selected cell Up   move cursor up or previous cell
Shift-M   merge cell below Down   move cursor down or next cell
S   Save and Checkpoint Shift   ignore
Ctrl-S   Save and Checkpoint
L   toggle line numbers
O   toggle output
Shift-O   toggle output scrolling
Esc   close pager
Q   close pager
H   show keyboard shortcut help
I,I   interrupt kernel
0,0   restart kernel
Space   scroll down
Shift-Space   scroll up
Shift   ignore

 

□ 기타 명령 모음

  - ipynb 파일 → py 파일 변환

    콘솔창에서 아래 명령 수행


    jupyter nbconvert --to script [파일명].ipynb



반응형

+ Recent posts