Home > Software design >  how can I print out values above average and return its years in numpy?
how can I print out values above average and return its years in numpy?

Time:05-31

i have a dataset which requires me to print out values larger than the average with the years. I'm able to retrieve the datas but not together with the years. is there any efficient way i can use to display both datas together? below is a snippet of the dataset with years and datas respectively.

[2010,2011,2012,2013,2014,2015,2016,2017] [29057,30979,31746,32964,31738,31010,31158,28736]

can someone please suggest a method for this! appreciate it, cheers!

CodePudding user response:

Since the question is not really explained I am not sure that is exactly what you except :

date = [2010,2011,2012,2013,2014,2015,2016,2017]
data = [29057,30979,31746,32964,31738,31010,31158,28736]

date_mean = np.mean(date)

for i,year in enumerate(date):
    if year > date_mean:
        print(year, data[i])

It print the following :

2014 31738
2015 31010
2016 31158
2017 28736

CodePudding user response:

Using numpy:

years = np.array([2010,2011,2012,2013,2014,2015,2016,2017])
data = np.array([29057,30979,31746,32964,31738,31010,31158,28736])

out = np.stack([years, data])[:, data>data.mean()]

output:

array([[ 2011,  2012,  2013,  2014,  2015,  2016],
       [30979, 31746, 32964, 31738, 31010, 31158]])

CodePudding user response:

You can select the date with a condition in array and the get the corresponding x with.index()

date = [2010,2011,2012,2013,2014,2015,2016,2017]

x = [29057,30979,31746,32964,31738,31010,31158,28736]

year = [d for d in date if d >= (np.mean(date))]

for d in year:
    
    print(d, x[date.index(d)])

>>>2014 31738
2015 31010
2016 31158
2017 28736

CodePudding user response:

np.c_[y[x>x.mean()], x[x>x.mean()]]

#output:
array([[ 2011, 30979],
       [ 2012, 31746],
       [ 2013, 32964],
       [ 2014, 31738],
       [ 2015, 31010],
       [ 2016, 31158]])

CodePudding user response:

To group your two lists together, you could use zip.

Example:

years = [2010,2011,2012,2013,2014,2015,2016,2017]
values = [29057,30979,31746,32964,31738,31010,31158,28736]
pair = list(zip(years, values))

Output:

>>> pair
[(2010, 29057), (2011, 30979), (2012, 31746), (2013, 32964), (2014, 31738), (2015, 31010), (2016, 31158), (2017, 28736)]

To get the values above the mean, you can use the following snippet:

above_mean = [i for i in pair if i[1] > sum(values)/len(values)]

Output:

>>> above_mean
[(2011, 30979), (2012, 31746), (2013, 32964), (2014, 31738), (2015, 31010), (2016, 31158)]
  • Related