約定:
import numpy as np
import pandas as pd
- 1
- 2
- 3
一、CSV數據的導入和保存
csv數據一般格式為逗號分隔,可在excel中打開展示。
示例 data1.csv:
A,B,C,D
1,2,3,a
4,5,6,b
7,8,9,c
- 1
- 2
- 3
- 4
- 5
代碼示例:
# 當列索引存在時
x = pd.read_csv("data1.csv")
print x
'''
A B C D
0 1 2 3 a
1 4 5 6 b
2 7 8 9 c
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
示例data2.csv:
1,2,3,a
4,5,6,b
7,8,9,c
- 1
- 2
- 3
- 4
代碼示例:
# 當列索引不存在時,默認從0開始索引
x = pd.read_csv('data2.csv', header=None)
print x
'''
0 1 2 3
0 1 2 3 a
1 4 5 6 b
2 7 8 9 c
'''
# 設置列索引
x = pd.read_csv('data2.csv',names=['A','B','C','D'])
print x
'''
A B C D
0 1 2 3 a
1 4 5 6 b
2 7 8 9 c
'''
# 將一(多)列的元素作為行(多層次)索引
x = pd.read_csv('data2.csv',names=['A','B','C','D'],index_col='D')
print x
'''
A B C
D
a 1 2 3
b 4 5 6
c 7 8 9
'''
x = pd.read_csv('data2.csv',names=['A','B','C','D'],index_col=['D','C'])
print x
'''
A B
D C
a 3 1 2
b 6 4 5
c 9 7 8
'''
- 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
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
示例data3.csv:
A,B,C,D
1,2,3,
NULL,5,6,b
7,nan,Nan,c
- 1
- 2
- 3
- 4
- 5
代碼示例:
# 一般NULL nan 空格 等自動轉換為NaN
x = pd.read_csv('data3.csv', na_values=[])
print x
'''
A B C D
0 1.0 2.0 3 NaN
1 NaN 5.0 6 b
2 7.0 NaN Nan c
'''
# 將某個元素值設置為NaN
x = pd.read_csv('data3.csv', na_values=['Nan'])
print x
'''
A B C D
0 1.0 2.0 3.0 NaN
1 NaN 5.0 6.0 b
2 7.0 NaN NaN c
'''
# 在對應列上設置元素為NaN
setNaN = {'C':['Nan'],'D':['b','c']}
x = pd.read_csv("data3.csv",na_values=setNaN)
print x
'''
A B C D
0 1.0 2.0 3.0 NaN
1 NaN 5.0 6.0 NaN
2 7.0 NaN NaN NaN
'''
# 保存數據到csv文件
x.to_csv('data3out.csv')
'''
data3out:
,A,B,C,D
0,1.0,2.0,3.0,
1,,5.0,6.0,
2,7.0,,,
'''
# 保存數據到csv文件,設置NaN的表示,去掉行索引,去掉列索引(header)
x.to_csv('data3out.csv',index=False,na_rep='NaN',header=False)
'''
data3out:
1.0,2.0,3.0,NaN
NaN,5.0,6.0,NaN
7.0,NaN,NaN,NaN
'''
x = pd.read_csv("data3out.csv",names=['W','X','Y','Z'])
print x
'''
W X Y Z
0 1.0 2.0 3.0 NaN
1 NaN 5.0 6.0 NaN
2 7.0 NaN NaN NaN
'''
- 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
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
二、txt數據的導入
txt文件中的數據通常以多個空格或者逗號等分割開。
示例data4.txt:
A B C
a 1 2 3
b 4 5 6
- 1
- 2
- 3
- 4
代碼示例:
# 讀取數據
x = pd.read_table('data4.txt', sep='\s+') # sep:分隔的正則表達式
print x
'''
A B C
a 1 2 3
b 4 5 6
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
示例data5.txt:
1.176813 3.167020
-0.566606 5.749003
0.931635 1.589505
-0.036453 2.690988
- 1
- 2
- 3
- 4
- 5
代碼示例:
# 使用numpy讀取txt
x = np.loadtxt('data5.txt', delimiter='\t') # 分隔符
print x
'''
[[ 1.176813 3.16702 ]
[-0.566606 5.749003]
[ 0.931635 1.589505]
[-0.036453 2.690988]]
'''
