1. 程式人生 > >Numpy與Pandas排序

Numpy與Pandas排序

資料格式

#lat,lng,rate
120.1513744 30.29040603 0.11
120.27504851 30.31321546 0.03
120.18916748 30.23685666 0.08
    center_list  = []
    for line in centers:
        lng,lat,rate = line.split(" ")
        center_list.append((lng,lat,rate))

    dtype = [('lng', float), ('lat', float), ('rate', float)]
    centers = np.array(center_list,dtype = dtype)
    centers = np.sort
(centers,order='rate') #按照第三列正序排序 #centers = np.sort(centers,order='rate')[::-1] #按照第三列逆序排序
import pandas as pd
from pandas import DataFrame,Series

columns = ['lng','lat','rate']
df = pd.read_table(fn,sep=' ',names= columns)

df.sort_values(by='rate')
#df.sort_values(by='rate',ascending = True
)