[데이터 기초] Pandas 2
DataFrame
- 가로줄: index
- 세로줄: columns
import pandas as pd
d = [1,2,3,4,5]
d_df = pd.DataFrame(d)
d_df
0 | |
0 | 1 |
1 | 2 |
2 | 3 |
3 | 4 |
4 | 5 |
[컬럼 이름 변경하기]
1. rename 사용
** pandas에서는 기본적으로 변경에 대한 메서드들을 바로 적용하지 않기 때문에 inplace=True를 걸어줘야 함
d_df.rename(columns = {0:'col1'},inplace = True)
d_df
col1 | |
0 | 1 |
1 | 2 |
2 | 3 |
3 | 4 |
4 | 5 |
2. 세로 인덱스(columns) 접근해서 수정
d_df.columns = ['col1']
d_df
col1 | |
0 | 1 |
1 | 2 |
2 | 3 |
3 | 4 |
4 | 5 |
[새로운 컬럼 생성하기]
d_df['newcol2'] = d
d_df
col1 | newcol2 | |
0 | 1 | 1 |
1 | 2 | 2 |
2 | 3 | 3 |
3 | 4 | 4 |
4 | 5 | 5 |
d_df['newcol3'] = d_df['col1'] + d_df['newcol2']
d_df
col1 | newcol2 | newcol3 | |
0 | 1 | 1 | 11 |
1 | 2 | 2 | 12 |
2 | 3 | 3 | 13 |
3 | 4 | 4 | 14 |
4 | 5 | 5 | 15 |
[열 삭제하기]
1. del 함수
del d_df['col1']
d_df
newcol2 | newcol3 | |
0 | 1 | 11 |
1 | 2 | 12 |
2 | 3 | 13 |
3 | 4 | 14 |
4 | 5 | 15 |
2. drop 메서드
- 가로줄 제거: axis = 0
- 세로줄 제거: axis = 1 -> 중간에 결측치 있는 경우 주의
** 마찬가지로 inplace=True 적용해야 원본이 변경됨
d_df.drop('newcol3', axis=1)
newcol2 | |
0 | 1 |
1 | 2 |
2 | 3 |
3 | 4 |
4 | 5 |
d_df.drop(2, axis = 0)
newcol2 | newcol3 | |
0 | 1 | 11 |
1 | 2 | 12 |
3 | 4 | 14 |
4 | 5 | 15 |
예시: 주식 가격 데이터프레임
stock_price_df = pd.DataFrame(
data = [10000, 10300, 9900, 10500, 11000],
index = ['2024-08-01','2024-08-02','2024-08-03','2024-08-04','2024-08-05']
)
stock_price_df.rename(columns = {0:'시가'}, inplace = True)
stock_price_df['종가'] = [11000, 12000, 10000, 13000, 12000]
stock_price_df['고가'] = [13000, 15000, 15000, 18000, 17000]
stock_price_df['저가'] = [3000, 5000, 5000, 8000, 7000]
stock_price_df
시가 | 종가 | 고가 | 저가 | |
2024-08-01 | 10000 | 11000 | 13000 | 3000 |
2024-08-02 | 10300 | 12000 | 15000 | 5000 |
2024-08-03 | 9900 | 10000 | 15000 | 5000 |
2024-08-04 | 10500 | 13000 | 18000 | 8000 |
2024-08-05 | 11000 | 12000 | 17000 | 7000 |
# 2024년 8월 3일 시가와 고가의 가격
stock_price_df.loc['2024-08-03',['시가','고가']]
stock_price_df.iloc[2,[0,2]]
참고) 원하는 컬럼/index가 어디에 위치하는지 찾는 메서드: get_loc()
stock_price_df.columns.get_loc('시가')
# 0
stock_price_df.index.get_loc('2024-08-02')
# 1
[인덱스 다루기]
1. 가로줄 인덱스 재구성: reindex
stock_price_df.reindex(['2024-08-01','2024-08-03']
시가 | 종가 | 고가 | 저가 | |
2024-08-01 | 10000 | 11000 | 13000 | 3000 |
2024-08-03 | 9900 | 10000 | 15000 | 5000 |
2. 가로줄 인덱스 초기화: reset_index()
stock_price_df.reset_index()
index | 시가 | 종가 | 고가 | 저가 | |
0 | 2024-08-01 | 10000 | 11000 | 13000 | 3000 |
1 | 2024-08-02 | 10300 | 12000 | 15000 | 5000 |
2 | 2024-08-03 | 9900 | 10000 | 15000 | 5000 |
3 | 2024-08-04 | 10500 | 13000 | 18000 | 8000 |
4 | 2024-08-05 | 11000 | 12000 | 17000 | 7000 |
[정렬]
1. 가로줄 index or 세로줄 columns 중심 정렬: sort_index(axis = 0/1)
stock_price_df.sort_index(axis=1, ascending=True)
index | 고가 | 시가 | 저가 | 종가 | |
0 | 2024-08-01 | 13000 | 10000 | 3000 | 11000 |
1 | 2024-08-02 | 15000 | 10300 | 5000 | 12000 |
2 | 2024-08-03 | 15000 | 9900 | 5000 | 10000 |
3 | 2024-08-04 | 18000 | 10500 | 8000 | 13000 |
4 | 2024-08-05 | 17000 | 11000 | 7000 | 12000 |
stock_price_df.sort_index(axis=0, ascending=False)
시가 | 종가 | 고가 | 저가 | |
2024-08-05 | 11000 | 12000 | 17000 | 7000 |
2024-08-04 | 10500 | 13000 | 18000 | 8000 |
2024-08-03 | 9900 | 10000 | 15000 | 5000 |
2024-08-02 | 10300 | 12000 | 15000 | 5000 |
2024-08-01 | 10000 | 11000 | 13000 | 3000 |
2. 값 자체로 정렬: sort_values()
stock_price_df.sort_values(by=['고가','시가'], ascending=False)
시가 | 종가 | 고가 | 저가 | |
2024-08-04 | 10500 | 13000 | 18000 | 8000 |
2024-08-05 | 11000 | 12000 | 17000 | 7000 |
2024-08-02 | 10300 | 12000 | 15000 | 5000 |
2024-08-03 | 9900 | 10000 | 15000 | 5000 |
2024-08-01 | 10000 | 11000 | 13000 | 3000 |
참고) rank
stock_price_df['종가'].rank(method='max', ascending=True)
종가 | |
2024-08-01 | 4.0 |
2024-08-02 | 3.0 |
2024-08-03 | 5.0 |
2024-08-04 | 1.0 |
2024-08-05 | 3.0 |
[줄 단위 연산을 통한 데이터 전처리]
apply + lambda: 줄 단위로 무엇을 할지에 대한 기술
** 하려는 일이 줄 단위로 동일한지 꼭 체크해야 한다!
# 날짜별로 최고가의 가격 보기
stock_price_df.apply(lambda x: x.max(), axis=1)
0 | |
2024-08-01 | 13000 |
2024-08-02 | 15000 |
2024-08-03 | 15000 |
2024-08-04 | 18000 |
2024-08-05 | 17000 |
# 날짜별 최고가와 최저가의 차이
stock_price_df.apply(lambda x: x.max() - x.min(), axis=1)
0 | |
2024-08-01 | 10000 |
2024-08-02 | 10000 |
2024-08-03 | 10000 |
2024-08-04 | 10000 |
2024-08-05 | 10000 |
참고) 도움이 되는 메서드
1. describe: 수치형 컬럼들에 대한 기본 정보 제공
2. corr: 컬럼 속성의 선형성에 대한 계량화
3. info: 데이터프레임의 기본 정보 확인
4. index.unique(): 유니크한 인덱스값만 출력
-> len과 함께 활용하여 데이터프레임의 인덱스가 유니크한지 체크할 수 있음