본문 바로가기

카테고리 없음

✋ [Python] Step 1. Pandas

Pandas의 기본 method 익히기 !

pd.read_csv()

  • csv 파일로 저장된 데이터를 pandas로 불러 오는 method
  • 다양한 확장자의 파일을 불러올 수 있음. ex) excel,sql,json
import pandas as pd
data_path = "/content/drive/MyDrive/YS_edu/data/"
titanic = pd.read_csv(data_path + "타이타닉.csv")

df.head(n) & data.tail(n)

  • df.head(n) = 첫번째 행부터 n 행까지 반환
  • df.tail(n) = 마지막 n 행 반환
titanic.head(8)
titanic.tail(8)

df.info()

  • df의 데이터 유형 확인 method
  • • 데이터 유형은 주로 DataFrame정수(int64), 실수(float64) 및 문자열(object) 으로 이루어져 있음.
titanic.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
 #   Column       Non-Null Count  Dtype  
---  ------       --------------  -----  
 0   PassengerId  891 non-null    int64  
 1   Survived     891 non-null    int64  
 2   Pclass       891 non-null    int64  
 3   Name         891 non-null    object 
 4   Sex          891 non-null    object 
 5   Age          714 non-null    float64
 6   SibSp        891 non-null    int64  
 7   Parch        891 non-null    int64  
 8   Ticket       891 non-null    object 
 9   Fare         891 non-null    float64
 10  Cabin        204 non-null    object 
 11  Embarked     889 non-null    object 
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB

df.describe()

  • 데이터의 기술 통계
  • • count : 값의 갯수, mean 값의 : 평균, std : 값의 표준 편차, min : 값의 최솟값, - max : 값의 최댓값
titanic.describe()

df.to_excel("titanic.xlsx", sheet_name="passengers", index=False)

  • titanic 데이터를 excel로 요청 했을때
  • to.excel() 데이터를 excel 로 저장
  • sheet 이름은 기본 값인 sheet1가 아닌 passengers 로 지정
  • index = False행의 레이블를 저장 하지 않겠다.
titanic.to_excel("titanic.xlsx", sheet_name="passengers", index=False)

 

reference !!

https://pandas.pydata.org/docs/getting_started/intro_tutorials/02_read_write.html