Pandas 자료구조 - Series와 DataFrame
Pandas 자료구조 - Series와 DataFrame
Pandas 자료구조 - Series와 DataFrame
Pandas
- Python 패키지
- 오픈 소스 데이터 분석 / 조직 도구
alias(별칭)
import pandas (as pd)
Pandas 자료구조
Series
- 1차원 배열, 인덱싱가능, 데이터타입 존재
- 생성
- numpy array로 생성한 경우
arr = np.arange(100,105) s = pd.Series(arr, dtype = 'int64') - list로 생성한 경우
s = pd.Series(['부장', '차장'])
- numpy array로 생성한 경우
- Index
s[0]: 부장s[-1]: error (음수색인불가)s = pd.Series(['마케팅', '경영'], index=['a', 'b'])
- Values
s.values: array([‘마케팅’, ‘경영’], dtype=object)
- ndim - 차원
s.ndim: 1 (1차원이라는 뜻)
- shape
- Series의 데이터 개수를 나타내며 tuple 형식
s.shape: (5,)
- NaN
s = pd.Series(['선화', '강호', np.nan, '소정', '우영'])s.isnull()ors.isna()로 결측치 확인 -> True/Falses[s.isnull]결측치를 가지고 있는 값들 확인 -> 값s.notnull()ors.notna()로 결측치 아닌 것 확인s[s.notnull]결측치를 가지고 있지 않은 값들 확인
- slicing
s[1:3]s['a':'b']
DataFrame
- 2차원 데이터, 행열구성, 데이터타입 존재
- 생성
- list로 생성한 경우
pd.DataFram([[1,2,3],[4,5,6]])pd.DataFram([[1,2,3],[4,5,6]], columns = ['가',나'])
- dictionary로 생성한 경우
data = { 'name' : ['kim','Lee'], 'age':[24,27] }
- list로 생성한 경우
- 속성
df.indexdf.columnsdf.valuesdf.dtypesdf.T
- index지정
- df.index = list(‘abc’)
- column 다루기
df['name']: 1개의 columndf[['name','age']]: 다중 columndf.rename(columns={'name': '이름'}): 컬럼명 변경(1)df.rename({'name': '이름'}, axis=1): 컬럼명 변경(2)df.rename(columns={'name': '이름'}, inplace=True)
=> 위에 rename 한 것들은 전부 일시적인데 inplace=True를 사용해주면 바로 df에 적용시켜서 다시 df 할당 필요없음
This post is licensed under CC BY 4.0 by the author.