본문 바로가기

Tech Stack/AICE - Associate

AICE - Associate - Python 기본 2 - pandas

import pandas as pd

 

DataFrame명 = pd.read_csv(파라미터)

 

filepath

seq

encoding

 

df.head

df.tail(n=10)

정의 가능

 

shape

columns

info

describbe

dtype

 

loc

이름 라벨 기준으로 슬라이싱

데이터프레임을 기준으로 name, age 라벨자체를 가져올 수 있음

iloc

동일하게 name age같은게 있을 때 0, 1 번호 기준으로 가져올 수 있음

 

column추가 삭제

 

import padas as pd

import numpy ad np

 

판다스는 대소문자를 구분 함

 

a1 = pd.DataFram

 

딕셔니러형, 리스트형으로 데이터 프레임 생성 가능

 

파일 읽어서 하려면

cust = pd.read_csv('./~~', encoding = "cp949")

 

shape : 속성 값 반환

columns 컬럼 명 확인

info 구조확인

describe 요약 통계량

dtypes 데이터 형태의 종류

 

cust.shape

 

cust.colums

 

cust.info()

 

cust.describe()

 

cust.dtype()

 

sep

index_col

해당 컬럼 인덱스 설정

usecols 로딩할 데이터 컬럼만 설정할 수 있음

 

데이터 추출

cust.cust_class = cust['cust_class']

 

복수의 컬럼 선택하기

cust[['s', 's', 's']]

 

특정 행 범위 [] 사용

 

789행 가져옴

cust[7:10]

 

cp = np.arange(10, 20)

cp

 

cust.index = np.arange(100, 10030)

cust

 

한개의 행

cust.loc[[289]]
세개의 행

cust.loc[[102, 202, 302]]

 

cust.iloc[[2, 102, 202]]

 

# 조건문이 너무 길어지거나 복잡해지면...아래와 같은 방식으로 해도 무방함
# 남자이면서 
sex = cust['sex_type']=='M'
# 3개월 평균 청구 금액이 50000 이상이면서 100000 미만
bill = (cust['r3m_avg_bill_amt']>=50000) & (cust['r3m_avg_bill_amt']< 100000)

cust[sex & bill].head()

 

 

컬럼 추가하기

 

# r3m_avg_bill_amt 두배로 새로운 col만들기
cust['r3m_avg_bill_amt2'] = cust['r3m_avg_bill_amt'] * 2
cust.head()
# 기존에 col을 연산하여 새로운 데이터 생성
cust['r3m_avg_bill_amt3'] = cust['r3m_avg_bill_amt2'] + cust['r3m_avg_bill_amt']
cust.head()



# 새로은 col들은 항상맨뒤에 존재 원하는 위치에 col을 추가하고자 하는 경우
# 위치를 조절 하고 싶다면(insert함수 사용)
cust.insert(10, 'r3m_avg_bill_amt10', cust['r3m_avg_bill_amt'] *10)  # 0부터 시작하여 10번째 col에 insert
cust.head()