2015-08-19 1386 views

回答

0

第一个import csv,那么你可以使用这段代码来打开你的csv文件。 通过行for row in reader:,您可以遍历csv文件中的行并使用您的代码执行任何您需要的操作。

import csv 
with open('your_file.csv', 'rb') as f: 
    reader = csv.reader(f) 
    for row in reader: 
     #do something 
6

而如果你需要符合csv文件行工作,你可以使用csv模块,该pandasmatplotlib模块提供的数据分析任务的高级接口。

data.csv

x,y 
1,2 
2,4 
3,6 
4,7 
5,11 
6,12 
7,13 
8,20 
9,17 
10,19 

plots.py

import pandas as pd 
import matplotlib.pyplot as plt 
df = pd.read_csv("data.csv") 
df.plot() # plots all columns against index 
df.plot(kind='scatter',x='x',y='y') # scatter plot 
df.plot(kind='density') # estimate density function 
# df.plot(kind='hist') # histogram 

输出

enter image description here

它是如何工作

df = pd.read_csv("data.csv") 

read_csv()读取CSV文件转换成Pandas Dataframe

数据帧图法是围绕matplotlib的情节的包装,是documented here

请注意,我们可以通过调整kind=关键字参数df.plot()得到不同类型的地块。直方图可用,在新版本的matplotlib比在这里安装,kind='hist'