这三种方法中最后一种最简单,不过花费时间比较长一点,第一种最麻烦,不过用时最短。这个可以通过ipython中的magic函数%%timeit来看。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#原始的方式 | |
lines = [line.split(',') for line in open('iris.csv')] | |
df = [[float(x) for x in line[:4]] for line in lines[1:]] | |
#使用numpy包 | |
import numpy as np | |
lines = np.loadtxt('iris.csv',delimiter=',',dtype='str') | |
df = lines[1:,:4].astype('float') | |
#使用pandas包 | |
import pandas as pd | |
df = pd.read_csv('iris.csv') | |
df=df.ix[:,:4] |
没有评论:
发表评论