1.引入pandas库
import pandas as pd
通过pd.调用方法
2.读取csv文件
data = pd.read_csv(filePath+fileName)
文件用逗号分隔
例如:
SYSNM,DS_SYSNM,SUBOBJNM,OBJNM,VALUE,TS,DURATION
A0402003,A0402003,CPU,CPU_UTIL,0,2017-10-25 00:00:16,60
A0402003,A0402003,CPU,CPU_UTIL,0,2017-10-25 00:01:11,60
A0402003,A0402003,CPU,CPU_UTIL,0,2017-10-25 00:02:06,60
A0402003,A0402003,CPU,CPU_UTIL,0,2017-10-25 00:03:01,60
A0402003,A0402003,CPU,CPU_UTIL,0.01,2017-10-25 00:03:56,60
首行传入 作为 columns
print('data.columns:{}'.format(data.columns))#.csv文件首行作为列名
print('data.index:{}'.format(data.index))#行号作为索引 从0开始
print('len(data):{}'.format(len(data)))#data长度 不包括首行
data.rename(columns={'TS': 'TS_2'}, inplace=True) #修改列名为TS_2
print('after rename() data.columns:{} '.format(data.columns))
data.rename(columns={'SYSNM': 'SYSNM_2','TS_2': 'TS'}, inplace=True) #修改多个列名
print('after rename() data.columns:{} '.format(data.columns))
print('print data by columns name = TS:{}'.format(data['TS']))#根据columns name 输出列
print('print data by columns index = 5:{}'.format(data.ix[:,5]))#根据columns index 输出第六列
print('print data by line index = 3:{}'.format(data.ix[3,:]))#根据columns index 输出第4行 索引从0开始
print('print data by line index = [0,2] and colunms name = [TS,VALUE]:{}'.format(data.ix[0:2,['TS', 'VALUE']]))#输出1-3行 TS、VALUE列数据
输出
pydev debugger: starting (pid: 10864)
data.columns:Index(['SYSNM', 'DS_SYSNM', 'SUBOBJNM', 'OBJNM', 'VALUE', 'TS', 'DURATION'], dtype='object')
data.index:RangeIndex(start=0, stop=5, step=1)
len(data):5
after rename() data.columns:Index(['SYSNM', 'DS_SYSNM', 'SUBOBJNM', 'OBJNM', 'VALUE', 'TS_2', 'DURATION'], dtype='object')
after rename() data.columns:Index(['SYSNM_2', 'DS_SYSNM', 'SUBOBJNM', 'OBJNM', 'VALUE', 'TS', 'DURATION'], dtype='object')
print data by columns name = TS:0 2017-10-25 00:00:16
1 2017-10-25 00:01:11
2 2017-10-25 00:02:06
3 2017-10-25 00:03:01
4 2017-10-25 00:03:56
Name: TS, dtype: object
print data by columns index = 5:0 2017-10-25 00:00:16
1 2017-10-25 00:01:11
2 2017-10-25 00:02:06
3 2017-10-25 00:03:01
4 2017-10-25 00:03:56
Name: TS, dtype: object
print data by line index = 3:SYSNM_2 A0402003
DS_SYSNM A0402003
SUBOBJNM CPU
OBJNM CPU_UTIL
VALUE 0
TS 2017-10-25 00:03:01
DURATION 60
Name: 3, dtype: object
print data by line index = [0,2] and colunms name = [5,6]: TS VALUE
0 2017-10-25 00:00:16 0.0
1 2017-10-25 00:01:11 0.0
2 2017-10-25 00:02:06 0.0
3.赋值
批量列赋值
data['newColumn'] = 'new value'#追加一列 赋值
print('data append new column:{}'.format(data['newColumn']))
length = len(data)
data['newColumn_2'] = pd.Series(np.array([1,2,3,4,5]))
print('data append new column:{}'.format(data['newColumn_2']))
输出
pydev debugger: starting (pid: 7220)
data append new column:0 new value
1 new value
2 new value
3 new value
4 new value
Name: newColumn, dtype: object
data append new column:0 1
1 2
2 3
3 4
4 5
Name: newColumn_2, dtype: int32
4.数据类型
data.dtypes
参考http://www.cnblogs.com/en-heng/p/5630849.html