[파이썬기초] pandas 데이터 읽기
2020. 3. 24. 17:59ㆍ노트/Python : 프로그래밍
pandas.read_csv
Parameters
- sep str, default ‘,’데이터의 구분자 지정
pd.read_table('examples/ex1.csv', sep=",")
- header int, list of int, default ‘infer’첫번째 행을 header로 지정하지 않도록 할 때 주로 씀.
혹은 columns이름으로 사용할 row 번호 지정
또는 시작 데이터로 사용할 행 Number 지정.
pd.read_table('examples/ex1.csv', header=None)
- names array-like, optional
columns 이름 직접 지정하고 싶을 때,
pd.read_csv('examples/ex2.csv', names=['a','b','c','d','message'])
- index_col int, str, sequence of int / str, or False, default None
데이터 프레임의 열 이름, data를 행 이름으로 사용하고 싶을 때
첫번째 열을 행이름(index)로 지정하고 싶을 때,
pd.read_csv('examples/ex2.csv',names=names, index_col='message')
pd.read_excel("영화평점.xlsx",header=None, index_col=0)
- skiprows list-like, int or callable, optional
건너 뛸 행(rows)를 설정할 때,
pd.read_csv('examples/ex4.csv', skiprows=[0,2,3])
- nrows int, optional
파일 전체를 읽는 대신 처음 몇줄만 읽어보고 싶을 때,
pd.read_csv('examples/ex6.csv', nrows=5)
- na_values scalar, str, list-like, or dict, optional
결측값이라고 인식해야되는 data를 지정할 때,
pd.read_csv("test_txt",sep="|",na_values=["없음","모름"])
- dtype Type name or dict of column -> type, optional
데이터 타입을 직접 지정하고 싶을 때,
pd.read_csv("test.txt", sep="|", dtype={"ID": int, "LAST_NAME": str, "AGE":str})
#dtype : dictionary 구조로 작성하여 열 이름과 데이터 타입 매칭
- encoding str, optional
Encoding to use for UTF when reading/writing (ex. ‘utf-8’)
- to_csv 메서드
데이터프레임을 텍스트 형식으로 기록할때, ( 쉼표로 구분된 형식으로 파일에 기록 )
data=pd.read_csv('examples/ex5.csv')
data.to_csv('examples/out.csv')
# 로우와 컬럼 이름을 포함하지 않으려면
data.to_csv('examples/out.csv', index=False, header=False)
#MS 엑셀 파일 데이터 읽어오기
- xls 파일 읽기 : xlrd 패키지 선 설치
> Anaconda prompt에서 명령어 : pip install xlrd 실행 - xlsx 파일 읽기 : openpyxl 패키지 선 설치
> Anaconda prompt에서 명령어 : pip install openpyxl 실행
frame=pd.read_excel("examples/ex1.xlsx", "Sheet1")
#pandas 데이터를 엑셀파일로 저장하고 싶을때,
frame.to_excel('examples/ex2.xlsx', 'Sheet1')
https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html?highlight=read_csv
pandas.read_csv — pandas 1.0.3 documentation
Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition,
pandas.pydata.org
참고: 『Python for Data Analysis』 p240~p263
작업경로 설정
import os
os.getcwd() #현재 작업경로 확인
>>>'C:\\Users\\student'
os.chdir('C:\\Users\\student\\Desktop\\') # 작업경로 변경
'노트 > Python : 프로그래밍' 카테고리의 다른 글
[파이썬기초] 데이터 정보 확인 및 참조 (0) | 2020.03.31 |
---|---|
[Kaggle] TED-Talk 토픽모델링 (Topic modeling) (0) | 2020.03.28 |
[파이썬] 두 점사이의 거리 구하기 코드 (0) | 2020.03.28 |
[딥러닝] 신경망 구현 코드 (0) | 2020.03.26 |
[파이썬기초] 주피터 노트북 테마 및 디자인 변경 (0) | 2020.03.23 |