【发布时间】:2017-01-17 12:34:13
【问题描述】:
我有 41 年的数据集,包括 10 列,我试图用 Matplotlib 绘制这些数据,并且我能够无错误地绘制列。但是,我想生成不同类型的图表,例如年平均 pcp 、月平均 pcp 和 pcp 年总和等。我从列中获取数据,我无法将这些数据转换为整数来进行一些计算。
这是来自 Csv 文件的示例数据集:
date day month year pcp1 pcp2 pcp3 pcp4 pcp5 pcp6
1.01.1979 1 1 1979 0.431 2.167 9.375 0.431 2.167 9.375
2.01.1979 2 1 1979 1.216 2.583 9.162 1.216 2.583 9.162
3.01.1979 3 1 1979 4.041 9.373 23.169 4.041 9.373 23.169
4.01.1979 4 1 1979 1.799 3.866 8.286 1.799 3.866 8.286
5.01.1979 5 1 1979 0.003 0.051 0.342 0.003 0.051 0.342
6.01.1979 6 1 1979 2.345 3.777 7.483 2.345 3.777 7.483
7.01.1979 7 1 1979 0.017 0.031 0.173 0.017 0.031 0.173
8.01.1979 8 1 1979 5.061 5.189 43.313 5.061 5.189 43.313
这是我的代码:
import csv
import numpy as np
import matplotlib.pyplot as plt
with open('output813b.csv', 'r') as csvfile:
# get number of columns
for line in csvfile.readlines():
array = line.split()
first_item = array[0]
num_columns = len(array)
csvfile.seek(0)
reader = csv.reader(csvfile, delimiter=',')
next(reader)
included_col1 = [1]
included_col6=[4]
x=[]
y=[]
for row in reader:
content = list(row[i] for i in included_col1)
content2= list(row[i] for i in included_col6)
x.append(content)
y.append(content2)
klm=tuple(x[i] for i in range(1,1000) if x[i]==["1979"])
s=0
for i in range(1,len(klm)):
s+=y[i] #### error (for +=: 'int' and 'list')
print (tuple(y[5])) ###example output ('2.345',)
print (int(y [5][0])) #### error invalid literal for int() with base 10: '2.345'
元组中的元组,我使用int(y[5][0]) 将元组转换为int,但出现错误。我在我的代码中放入错误消息。我该如何解决这个问题并进行一些计算。提前致谢
【问题讨论】:
-
嗯...你不想让它成为
float,因为它有小数点吗?
标签: python csv numpy matplotlib