CS & DS/Numpy & Pandas
Pandas Data Loading 판다스 데이터 적재
EthanJ
2022. 11. 5. 19:31
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=None
settingall 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 |