I hope you can use some built-in packages, such as
datetime, cause it’s useful when working with datetime objects.
import numpy as np import datetime as dt df = np.genfromtxt('test_files/COX_SNOW-1.csv', delimiter=',', skip_header=1, dtype=str) date = np.array([dt.datetime.strptime(d, "%Y%m%d").month for d in df[:, 0]]) snow = df[:, 1].copy().astype(np.int32) has_snowed = snow > 0 for month in range(1, 13): month_str = dt.datetime(year=1, month=month, day=1).strftime('%B') how_much_snow = len(snow[has_snowed & (date == month)]) print(month_str, ':', how_much_snow)
I loaded the data as
str so we guarantee we can parse the
Date column as dates later on. That’s why we also need to explicitly convert the
snow column to
int32, otherwise the
> comparison won’t work.
The output is as follows:
January : 13 February : 19 March : 20 April : 13 May : 8 June : 9 July : 2 August : 7 September : 9 October : 19 November : 16 December : 15
Let me know if this worked for you or if you have any further questions.
CLICK HERE to find out more related problems solutions.