熊猫数据框排序

时间:2018-01-26 14:34:12

标签: python pandas sorting dataframe

我有一个像这样的pandas数据框,我尝试按列'dist'排序。排序的数据帧应以E或F开头,如下所示。我使用sort_values,它不适合我。该函数计算从“开始”位置到位置列表的距离['C','B','D','E','A','F']然后应该按升序对数据帧进行排序使用'dist'列订购。 有人可以告诉我为什么排序不起作用吗?

locations = {'Start':(20,5),'A':(10,3), 'B':(5,3), 'C':(5, 7), 'D':(10,7),'E':(14,4),'F':(14,6)}

    loc_list
Out[194]: ['C', 'B', 'D', 'E', 'A', 'F']

def closest_locations(from_loc_point, to_loc_list):
    lresults=list()
    for list_index in range(len(to_loc_list)):
        dist= hypot(locations[from_loc_point[0]][0] -locations[to_loc_list[list_index]][0],locations[from_loc_point[0]][1] -locations[to_loc_list[list_index]][1]) # cumsum distante
        lista_dist = [from_loc_point[0],to_loc_list[list_index],dist]
        lresults.append(lista_dist[:])
    RESULTS = pd.DataFrame(np.array(lresults))
    RESULTS.columns = ['from','to','dist']
    RESULTS.sort_values(['dist'],ascending=[True],inplace=True)
    RESULTS.index = range(len(RESULTS))
    return RESULTS

closest_locations(['Start'], loc_list)
Out[189]: 
    from to                dist
0  Start  D   10.19803902718557
1  Start  A   10.19803902718557
2  Start  C  15.132745950421555
3  Start  B  15.132745950421555
4  Start  E    6.08276253029822
5  Start  F    6.08276253029822

closest_two_loc.dtypes 出[247]:

from    object
to      object
dist    object
dtype: object

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?

locations = {'Start':(20,5),'A':(10,3), 'B':(5,3), 'C':(5, 7), 'D':(10,7),'E':(14,4),'F':(14,6)}
df= pd.DataFrame.from_dict(locations, orient='index').rename(columns={0:'x', 1:'y'})
df['dist'] = df.apply(lambda row: pd.np.sqrt((row['x'] - df.loc['Start', 'x'])**2 + (row['y'] - df.loc['Start', 'y'])**2), axis=1)
df.drop(['Start']).sort_values(by='dist')
    x  y       dist
E  14  4   6.082763
F  14  6   6.082763
A  10  3  10.198039
D  10  7  10.198039
C   5  7  15.132746
B   5  3  15.132746

或者如果你想把它包装在一个函数

def dist_from(df, col):
    df['dist'] = df.apply(lambda row: pd.np.sqrt((row['x'] - df.loc[col,'x'])**2 + (row['y'] - df.loc[col, 'y'])**2), axis=1)
    df['form'] = col
    df.drop([col]).sort_values(by='dist')
    df.index.name = 'to'
    return df.reset_index().loc[:, ['from', 'to', 'dist']]

答案 1 :(得分:0)

您需要转换" dist"中的值要浮动的列:

df = closest_locations(['Start'], loc_list)
df.dist = list(map(lambda x: float(x), df.dist)) # convert each value to float
print(df.sort_values('dist'))                    # now it will sort properly

输出:

    from to       dist
4  Start  E   6.082763
5  Start  F   6.082763
0  Start  D  10.198039
1  Start  A  10.198039
2  Start  C  15.132746
3  Start  B  15.132746

编辑:正如@jezrael在评论中提到的,以下是一种更直接的方法:

df.dist = df.dist.astype(float)