Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- 나이브베이즈
- Naive Bayes
- 사이킷런
- ML
- 머신러닝
- 순회 크롤러
- 파이썬 크롤링
- python
- 파이썬 크롤러
- NumPy
- scikit-learn
- Machine Learning
- sklearn
- python control statement
- KMeans Clustering
- control statement
- dataframe
- 배열
- 파이썬 객체 지향 프로그래밍
- 판다스
- 파이썬
- Python crawler
- 제어문
- 타이타닉 데이터
- pandas
- 넘파이
- Titanic data set
- 파이썬 제어문
- Data pre-processing
- K평균군집화
Archives
- Today
- Total
Try to 개발자 EthanJ의 성장 로그
Pandas Data Loading 판다스 데이터 적재 본문
Pandas Data Loading 판다스 데이터 적재¶
1. *.csv file reading¶
pd.read_csv('file path')columns name이 존재하는 data
columns name이 없는 data
separator 설정
주석(comment)이 있는 data
In [1]:
import pandas as pd
절대경로(Absolute path)
- An absolute path is defined as the specifying the location of a file or directory from the root directory(/).
상대경로(Relative path)
- Relative path is defined as the path related to the present working directly(pwd).
It starts at your current directory and never starts with a / .
- Relative path is defined as the path related to the present working directly(pwd).
1.1. columns name이 존재하는 data¶
- `pd.reas_csv('file path') 기본 동작: 첫 row data를 columns로 사용
In [2]:
ex1_data = pd.read_csv('data/ex1.csv')
ex1_data
Out[2]:
| a | b | c | d | message | |
|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | hello |
| 1 | 5 | 6 | 7 | 8 | world |
| 2 | 9 | 10 | 11 | 12 | foo |
In [3]:
ex2_data = pd.read_csv('data/ex2.csv', header=None)
ex2_data
Out[3]:
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | hello |
| 1 | 5 | 6 | 7 | 8 | world |
| 2 | 9 | 10 | 11 | 12 | foo |
- columns name 'a', 'b', 'c', 'd', 'e' 부여
In [4]:
ex2_data.columns = list('abcde')
ex2_data
Out[4]:
| a | b | c | d | e | |
|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | hello |
| 1 | 5 | 6 | 7 | 8 | world |
| 2 | 9 | 10 | 11 | 12 | foo |
1.3. 구분자(separator) 설정¶
default separator:
,(comma)pd.read_csv('file path'): optional parametersep='separator': 구분자 설정
In [5]:
ex3_data = pd.read_csv('data/ex3.csv')
print(ex3_data.shape)
ex3_data
(4, 1)
Out[5]:
| A B C | |
|---|---|
| 0 | aaa -0.264438 -1.026059 -0.619500 |
| 1 | bbb 0.927272 0.302904 -0.032399 |
| 2 | ccc -0.264273 -0.386314 -0.217601 |
| 3 | ddd -0.871858 -0.348382 1.100491 |
default separator
,(comma)가 아닌 문자를 separator로 data 처리,,`,\t,`,/: white space > 전부\s+정규표현식으로 처리 가능
In [6]:
ex3_new = pd.read_csv('data/ex3.csv', sep='\s+')
ex3_new
Out[6]:
| A | B | C | |
|---|---|---|---|
| aaa | -0.264438 | -1.026059 | -0.619500 |
| bbb | 0.927272 | 0.302904 | -0.032399 |
| ccc | -0.264273 | -0.386314 | -0.217601 |
| ddd | -0.871858 | -0.348382 | 1.100491 |
In [7]:
pd.read_csv('data/ex4.csv')
Out[7]:
| # hey! | ||||
|---|---|---|---|---|
| a | b | c | d | message |
| # just wanted to make things more difficult for you | NaN | NaN | NaN | NaN |
| # who reads CSV files with computers | anyway? | NaN | NaN | NaN |
| 1 | 2 | 3 | 4 | hello |
| 5 | 6 | 7 | 8 | world |
| 9 | 10 | 11 | 12 | foo |
- optional parameter:
skiprows=[skip할 row index 번호 list]
In [8]:
pd.read_csv('data/ex4.csv', skiprows=[0, 2, 3])
Out[8]:
| a | b | c | d | message | |
|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | hello |
| 1 | 5 | 6 | 7 | 8 | world |
| 2 | 9 | 10 | 11 | 12 | foo |
- optional parameter:
comment='comment symbol'
In [9]:
pd.read_csv('data/ex4.csv', comment='#')
Out[9]:
| a | b | c | d | message | |
|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | hello |
| 1 | 5 | 6 | 7 | 8 | world |
| 2 | 9 | 10 | 11 | 12 | foo |
2. *.xlsx file reading¶
`pd.read_excel('file path')
Basic Logic: first sheet에 있는 data를 읽어와서
DataFrame으로 저장To read all of sheets, optional parameter
sheet_name=Nonesettingall of sheets를 읽어서
dict의 형식으로 저장key: sheet의 이름,value: 각 sheet에 있는 data들을 저장한DataFrame
To read specific sheet,
sheet_name='sheet_name'setting
- default: first sheet to DataFrame
In [10]:
pd.read_excel('data/ex6.xlsx')
Out[10]:
| a | b | |
|---|---|---|
| 0 | 1 | 2 |
| 1 | 5 | 6 |
| 2 | 9 | 10 |
- Reading second sheet: optional parameter
sheet_name='sheet name'
In [11]:
pd.read_excel('data/ex6.xlsx', sheet_name='시트2')
Out[11]:
| c | d | message | |
|---|---|---|---|
| 0 | 3 | 4 | hello |
| 1 | 7 | 8 | world |
| 2 | 11 | 12 | foo |
- Reading all of sheets:
sheet_name=None
In [12]:
ex6_data = pd.read_excel('data/ex6.xlsx', sheet_name=None)
ex6_data
Out[12]:
{'시트1': a b
0 1 2
1 5 6
2 9 10,
'시트2': c d message
0 3 4 hello
1 7 8 world
2 11 12 foo}
sheet_name=None으로 읽어 온 data type:dict
In [13]:
print(type(ex6_data))
print(ex6_data.keys())
print(ex6_data.values())
<class 'dict'> dict_keys(['시트1', '시트2']) dict_values([ a b 0 1 2 1 5 6 2 9 10, c d message 0 3 4 hello 1 7 8 world 2 11 12 foo])
시트1,시트2의 data를 각각의DataFrame변수에 저장
In [14]:
sheet1, sheet2 = ex6_data.values()
sheet1
Out[14]:
| a | b | |
|---|---|---|
| 0 | 1 | 2 |
| 1 | 5 | 6 |
| 2 | 9 | 10 |
In [15]:
sheet2
Out[15]:
| c | d | message | |
|---|---|---|---|
| 0 | 3 | 4 | hello |
| 1 | 7 | 8 | world |
| 2 | 11 | 12 | foo |
'CS & DS > Numpy & Pandas' 카테고리의 다른 글
| Pandas Data analysis with Baseball player 판다스 야구 선수 데이터 분석 (0) | 2022.11.12 |
|---|---|
| Pandas Data pre-processing 판다스 데이터 전처리 (0) | 2022.11.12 |
| Pandas DataFrame 판다스 데이터프레임 (0) | 2022.11.05 |
| Pandas Series 판다스 시리즈 (0) | 2022.11.03 |
| Numpy array Manipulation 넘파이 배열 변형 (0) | 2022.11.02 |
Comments
