选择.csv中的列并对其进行操作 [英] Selecting and operating on columns in a .csv

查看:312
本文介绍了选择.csv中的列并对其进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含38列和1500+行的csv,其中包含浮点数和字符串.我想从该集合中获取3列(x,y,z)浮点数据,以找到f=(x+y)/z的平均值.经过研究,我成功地将这些列隔离为numpy数组,并执行了f=(x+y)/z.现在,当我尝试求和f时,不会将数组相加.我打印f并且看到1500个正确值的项目,但不是这些总和的值.

I have a csv with 38 columns and 1500+ rows which contains floats and strings. I want 3 columns (x,y,z) of float data from this set to find the average of f=(x+y)/z. After research I successfully isolated these columns as numpy arrays and performed f=(x+y)/z. Now when I try to sum f the array isn't added up. I print f And I see 1500 items of correct values but not the sum of these.

  reader=csv.reader(open('myfile.csv' ,"rb"),delimiter=',')
  reader.next()
  reader.next()
  x=list(reader)
  data=numpy.array(x)
  rows=data.shape[0]
  for i in range (0,rows):
      x=numpy.array(data[i,18]).astype('float')
      y=numpy.array(data[i,19]).astype('float')
      z=numpy.array(data[i,6]).astype('float')
      f=numpy.array((x+y)/z)
      average=numpy.sum(f)/rows
      print average

推荐答案

如果data已经是数组,则不需要for循环:

If data is already an array, you don't need the for loop:

x = data[:, 18].astype(float)
y = data[:, 19].astype(float)
z = data[:, 6].astype(float)
f = (x+y) / z
average = np.average(f)

使用 np.loadtxt :

You would probably be better off by reading your file with np.loadtxt:

data = np.loadtxt('myfile.csv', dtype=float, delimiter=',' skiprows=2,
                  usecols=(6, 18, 19))

或直接获取xyz:

x, y, z = np.loadtxt('myfile.csv', dtype=float, delimiter=',' skiprows=2,
                     usecols=(6, 18, 19), unpack=True)

这篇关于选择.csv中的列并对其进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆