2016-12-01 156 views
3

我得到以下的數據幀:數據排序

Dis  System_num Energy 
0 0.9   1  -2.3108 
1 0.7   1  11.8735 
2 1.2   1  -2.3408 
3 2.0   1  -0.3485 
4 2.0   2  -0.9379 
5 0.7   2  7.4776 
6 1.5   2  -2.2877 
7 0.9   2  -4.1789 
8 2.0   3  -3.6596 
9 1.0   3  -18.4582 
10 0.9   3  -16.2202 
11 0.7   3  16.6290 

欲DIS的列值(升序),用於System_num每個數字排序,我的意思是:

0 0.7   1  11.8735 
1 0.9   1  -2.3108 
2 1.2   1  -2.3408 
3 2.0   1  -0.3485 
4 0.7   2  7.4776 
5 0.9   2  -4.1789 
6 1.5   2  -2.2877 
7 2.0   2  -0.9379 
8 0.7   3  16.6290 
8 0.9   3  -16.2202 
10 1.0   3  -18.4582 
11 2.0   3  -3.6596 

回答

1

使用sort_valuesSystem_num作爲第一列由

df.sort_values(['System_num', 'Dis']) 

排序

另一種方式來做到這一點,而不排序System_num

設置

df = pd.DataFrame([ 
     [ 2. , 2. , -0.9379], 
     [ 0.7 , 2. , 7.4776], 
     [ 1.5 , 2. , -2.2877], 
     [ 0.9 , 2. , -4.1789], 
     [ 0.9 , 1. , -2.3108], 
     [ 0.7 , 1. , 11.8735], 
     [ 1.2 , 1. , -2.3408], 
     [ 2. , 1. , -0.3485], 
     [ 2. , 3. , -3.6596], 
     [ 1. , 3. , -18.4582], 
     [ 0.9 , 3. , -16.2202], 
     [ 0.7 , 3. , 16.629 ] 
    ], columns=['Dis', 'System_num', 'Energy']) 

df.groupby('System_num', sort=False) \ 
    .apply(pd.DataFrame.sort_values, by='Dis') \ 
    .reset_index(drop=True) 

enter image description here

+0

謝謝!這非常有幫助! – Monica