Reputation: 43
I've got a data frame like
x y cluster
0 112 4
0 113 4
1 111 4
and I'll get locations from this code:
for n in range(0,9):
...
location = np.array(cluseter_location )
I want to sort it out in the order of column 'cluster' with small columns 'y' means, so I tried:
for n in range(0,9):
cluster_ = data2[data2['cluster_id']== n]
...
Upvotes: 0
Views: 59
Reputation: 19307
In your code, instead of appending the cluster_int, just append the tuple (n, cluster_int), and then later while sorting, use lambda to sort by the second value of each tuple.
for n in range(0,9):
cluster_ = data2[data2['cluster_id']== n]
cluster_list = cluster_['y'].tolist()
cluster_avg = sum(cluster_list)/len(cluster_list)
cluster_int = int(cluster_avg)
print("cluster_id : %d" %n ,"average : %d" %cluster_int)
lst.append((n,cluster_int)) #<-------
a = sorted(lst, key = lambda x:x[1]) #<-------
print(a) #<-------
ordered_average = [average for cluster, average in a] #<-------
ordered_clusters = [cluster for cluster, average in a] #<-------
print(ordered_average) #<-------
print(ordered_clusters) #<-------
#cluster and average together
[(4,112),(8,121,(1,127),(6,139),(5,149)]
#averages sorted
[112, 121, 127, 139, 149]
#clusters sorted
[4,8,1,6,5]
A quicker way to do this would be directly sorting the pandas dataframe after groupby.
print(df.groupby('cluster')['y'].mean().reset_index().sort_values('y'))
Upvotes: 1