Reputation: 1605
I am a newbie to python and numpy. I want to find the total rainfall days (ie. sum of column E for each year, attach the image herewith). I am using numpy.unique to find the unique elements of array year.
following is my attempt;
import numpy as np
data = np.genfromtxt("location/ofthe/file", delimiter = " ")
unique_year = np.unique(data[:,0], return_index=True)
print(unique_year)
j= input('select one of the unique year: >>> ')
#Then I want to give the sum of the rainfall days in that year.
I would appreciate if someone could help me. Thanks in advance.
Upvotes: 1
Views: 77
Reputation: 164673
For such tasks, Pandas (which builds on NumPy) is more easily adaptable.
Here, you can use GroupBy
to create a series mapping. You can then use your input to query your series:
import pandas as pd
# read file into dataframe
df = pd.read_excel('file.xlsx')
# create series mapping from GroupBy object
rain_days_by_year = df.groupby('year')['Rain days(in numbers)'].sum()
# get input as integer
j = int(input('select one of the unique year: >>> '))
# extract data
res = rain_days_by_year[j]
Upvotes: 1