Ragnar Lothbrok
Ragnar Lothbrok

Reputation: 1135

Python: Find Average Y-Value for Each X-Value in [X,Y] Coordinates

Let's say I have a list of x,y coordinates like this:

coordinate_list = [(4,6),(2,5),(0,4),(-2,-2),(0,2),(0,0),(8,8),(8,11),(8,14)]

I want to find the average y-value associated with each x-value. So for instance, there's only one "2" x-value in the dataset, so the average y-value would be "5". However, there are three 8's and the average y-value would be 11 [ (8+11+14) / 3 ].

What would be the most efficient way to do this?

Upvotes: 0

Views: 1667

Answers (3)

nandal
nandal

Reputation: 2634

Try the mean() function from statistics module with list comprehension

from statistics import mean

x0_filter_value = 0   # can be any value of your choice for finding average
result = mean([x[1] for x in coordinate_list if x[0] == x0_filter_value])
print(result)

And to print means for all X[0] values:

 for i in set([x[0] for x in coordinate_list]):
      print (i,mean([x[1] for x in coordinate_list if x[0] == i]))

Upvotes: 0

zanuda
zanuda

Reputation: 245

y_values_by_x = {}
for x, y in coordinate_list:
    y_values_by_x.setdefault(x, []).append(y)

average_y_by_x = {k: sum(v)/len(v) for k, v in y_values_by_x.items()}

Upvotes: 1

Kuldeep Singh Sidhu
Kuldeep Singh Sidhu

Reputation: 3856

You can use pandas

coordinate_list = [(4,6),(2,5),(0,4),(-2,-2),(0,2),(0,0),(8,8),(8,11),(8,14)]
import pandas as pd
df = pd.DataFrame(coordinate_list)
df
df.groupby([0]).mean()
| 0 |  |  1  |
| --- | --- |
| -2 | -2 |
| 0 | 2 |
| 2 | 5 |
| 4 | 6 |
| 8 | 11 |

Upvotes: 1

Related Questions