如何使用条件

时间:2017-01-25 11:41:12

标签: python loops matrix

我是python的新手并试图找出如何循环2x2矩阵。

我的起点是* .csv文件,其中包含大量数据(10列,173828行)。因为我只需要第一列(sigma_0值)和第六列(日期),所以我创建了一个名为sigma_JD的矩阵,它只包含这两列:

    import csv
    import numpy as np
    with open("C:/Users/.../03971822.csv") as input_file:
       reader = csv.reader(input_file)

       array = []
       for row in reader:
       array.append(row)

    matrix = np.asmatrix(array)
    idx_IN_columns = [0, 5]
    sigma_JD = matrix[:, idx_IN_columns]
    print(sigma_JD)
    print("size sigma_JD: ", np.shape(sigma_JD))

    >>> print(sigma_JD)
    [['-12.42' '2451544.576']
     ['-12.92' '2451544.576']
     ['-12.45' '2451544.576']
     ..., 
     ['-11.66' '2454688.389']
     ['-12.61' '2454688.389']
     ['-11.72' '2454688.389']]
    >>> print("size sigma_JD: ", np.shape(sigma_JD))
    size sigma_JD:  (173828, 2)

现在我想循环第二列 - 日期;它以特定的方式显示,它被称为"朱利安日",例如JD值

            2451544,5 = 01/January/2000 0:00
            2451545,5 = 02/January/2000 0:00
            2451546,5 = 03/January/2000 0:00

2451544告诉日/月/年,小数位表示时间。

我想编写一个代码,其中考虑了所有sigma_0值,这些值在一天之内。因此,循环应该使用特定值,而不是通过索引。

它应该以2451544,5开头,然后考虑所有sigma_0值,这些值在一天之内(并将其总结),然后转到第二天2451544,5并执行相同的操作....

我试过s.th.像这样,但它不起作用

    x = 2451544.5
    y = x + 1
    for i in sigma_JD[:, 1]:
        while x < y:
    print(sigma_JD[i, 1])
    break

然后我考虑创建自己的功能,但没有达到目的:

   def select(x):
   count = 2451544.5
   select = []  
   for i in range(0, len(x[:, 1])):   # loop over Julian Day
      if count < count + 1:
        row = []
        for j in range(0, len(x[:, 0])):   # loop over sigma_0 values
           # take all sigma_0 values and sum it up
        count += 1
   return select

如果有人可以帮助我,那将是非常非常好的。我正在研究这几天,这真让我感到震惊,因为我不知道如何完成这项工作。

非常感谢。

2 个答案:

答案 0 :(得分:0)

试一试(您可能需要下载jdcal package

import csv
import collections
from jdcal import jd2gcal

with open("test.csv") as input_file:
    reader = csv.reader(input_file)

    jd_sigma_map = collections.defaultdict(int)
    jd_sigma_count = collections.defaultdict(int)

    for row in reader:

        #convert to the normal date format
        year, month, dd, ms = jd2gcal(float(row[5]), 0)

        #use date as key
        date_key = '%s-%s-%s' % (year, month, dd)

        #Sum sigma values for same key (day)
        jd_sigma_map[date_key] += float(row[0])
        jd_sigma_count[date_key] += 1

测试文件(test.csv):

-12.42, 0, 0, 0, 0, 2451544.576
-12.92, 0, 0, 0, 0, 2451544.576
-5.92,  0, 0, 0, 0, 2451545.677
-2.92,  0, 0, 0, 0, 2451545.699
-16.61, 0, 0, 0, 0, 2454688.310
-11.66, 0, 0, 0, 0, 2454688.389
-12.61, 0, 0, 0, 0, 2454688.400

输出:

#For ordered (by date) output
ordered_dict = collections.OrderedDict(sorted(jd_sigma_map.items()))

for k, v in ordered_dict.items():
    average = float(v/jd_sigma_count[k])
    print("Sigma value for day %s = %0.3f \t(over %d days)\tAverage = %0.3f" 
          % (k, v, jd_sigma_count[k], average))

# Sigma value for day 2000-1-1 = -25.340  (over 2 days)   Average = -12.670
# Sigma value for day 2000-1-2 = -8.840   (over 2 days)   Average = -4.420
# Sigma value for day 2008-8-9 = -40.880  (over 3 days)   Average = -13.627

注意:

  • 使用字典可以让我们保持&#39;每天的西格玛值。使用2x2阵列比摆弄更容易。
  • 输出jd_sigma_map是一个字典,其中键是YYYY-MM-DD格式的日期,值是当天的总西格玛值。我们不关心格式,我们只希望一天中的每个键都唯一
  • 正如你所知,我做了所有的分析&#39;在飞行中&#39;在读取CSV期间,您也可以将其存储在列表中,并在文件关闭后进行分析。
  • Python 3解决方案(对于Python 2.x,将print()更改为print ...,将items()更改为iteritems()
  • 请参阅this question对输出字典进行排序(在代码中添加)

答案 1 :(得分:0)

import numpy as np

array = [['-12.42', '2451544.576'],
     ['-12.92', '2451544.576'],
     ['-12.45', '2451544.576'],
     ['-11.66', '2454688.389'],
     ['-12.61', '2454688.389'],
     ['-11.72', '2454688.389']]


matrix = np.asmatrix(array)
print matrix
for (i, j), ele in np.ndenumerate(matrix):
    if j == 1: #SECOND COL
         print i, j, ele