본문 바로가기
PYTHON

Pandas-Dataframe, csv 파일 읽어오기[PYTHON개발]

by cellin 2023. 11. 2.

pandas는 데이터 분석 라이브러리로 dataframe을 주로 다루기 위한 라이브러리이다

 

 

 

pandas 설치 명령어

 

pip install pandas

 

 

Create an alias with the as keyword while importing

 

import pandas as pd
 

 

 

Pandas Dataframe 만들기

 

EXAMPLE

mydataset = {
  'cars': ["BMW", "Volvo", "Ford"],
  'passings': [3, 7, 2]
}

myvar = pd.DataFrame(mydataset)

print(myvar)
 

결과

    cars  passings
0    BMW         3
1  Volvo         7
2   Ford         2
 

 

 

 

Dataframe에서 특정 컬럼이나 로우(인덱스) 선택하기

 

column을 조회할때 df['column'], row를 조회할 때는 df.ix[index]를 많이 사용

 

Dataframe생성

import pandas as pd

data = {
  "calories": [420, 380, 390],
  "duration": [50, 40, 45]
}

#load data into a DataFrame object:
df = pd.DataFrame(data)

print(df) 
 

RESULT

     calories  duration
  0       420        50
  1       380        40
  2       390        45
 

1) specific column or row select[선택하기]

#refer to the row index:
print(df.loc[0])
 

RESULT - Return 0

  calories    420
  duration     50
  Name: 0, dtype: int64
 

2) specific column or row select[선택하기]

#use a list of indexes:
print(df.loc[[0, 1]])
 

RESULT - Return row 0, 1

       calories  duration
  0       420        50
  1       380        40
 

 

 

csv파일 읽기

 

import pandas as pd

df = pd.read_csv('data.csv')

print(df.to_string())
 

 

 

 

 

 

 

 

**pandas버전 확인

import pandas as pd

print(pd.__version__)
 

 

 

 

 

 

 

 

 

사진: Unsplashbillow926

728x90
반응형