[업무 지식]/Python
[10분 판다스] python 기초
에디터 윤슬
2024. 10. 31. 09:56
DataFrame 만들기
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dates = pd.date_range('20241001', periods = 6)
df = pd.DataFrame(np.random.randn(6, 4), index = dates, columns = list('ABCD'))
df
새 열을 설정하면 데이터가 인덱스 별로 자동 정렬
s1 = pd.Series([1, 2, 3, 4, 5, 6], index = pd.date_range('20241001', periods = 6))
s1
df['F'] = s1
df
NUMPY 배열을 사용한 할당값 변경
df.loc[:, 'D'] = np.array([4] * len(df))
df
reindexing
- Reindexing으로 지정된 축 상의 인덱스를 변경 / 추가 / 삭제할 수 있다. Reindexing은 데이터의 복사본을 반환
df1 = df.reindex(index = dates[0:4], columns = list(df.columns) + ['E'])
df1
df1.loc[dates[0]:dates[1], 'E'] = 1
df1
isin으로 특정값이 있는 행 모두 불러오기
df1[df1['F'].isin([1, 4])]
df1