简体   繁体   中英

python pandas groupby sorting

I would like to sort the following dataset in descending order (largest to smallest) based on whether 'total_sales' is positive or negative for each state. For example, for AK, the total sales are positive for all 4 regions. So the regions would be sorted starting with the largest sales for all positive regions in descending order and then would move to the negative regions in descending order. This would be the reverse for NC, as NC has a negative 'total_sales' net number.

Also, if possible, in order to denote the first region/largest region that offsets that states 'total_sales' trend, I would like to create a column that states 'offset' for that first value that is counter to the overall 'total_sales' trend for each state.

State   Region   Sales    Total_sales    
AK      North    10       20
AK      South    25       20 
AK      East     -30      20
AK      West     15       20
NC      North    20       -35
NC      South    -50      -35
NC      East     -20      -35 
NC      West     15       -35

Desired Output

State   Region   Sales    Total_sales    Note
AK      South     25        20
AK      West      15        20 
AK      North     10        20
AK      East      -30       20           offset
NC      South     -50       -35
NC      East      -20       -35
NC      North     20        -35          offset
NC      West      15        -35

Thank you! Any help/direction you can provide is greatly appreciated!

First we need to get the sort value, by using np.sign create the sort key

df['sign']=np.sign(df.Total_sales)
df['sign']=df.sign*np.sign(df.Sales)
df['sign2']=df.Sales.abs()
df=df.sort_values(['State','sign','sign2'],ascending=[True,False,False])

Then we get the offset position

df['SaleSign']=np.sign(df.Sales)
df['note']=df.groupby('State').SaleSign.diff().fillna(0).ne(0).map({True:'offset',False:''})
df
Out[427]: 
  State Region  Sales  Total_sales  sign  SaleSign    note
1    AK  South     25           20     1         1        
3    AK   West     15           20     1         1        
0    AK  North     10           20     1         1        
2    AK   East    -30           20    -1        -1  offset
6    NC   East    -20          -35     1        -1        
5    NC  South    -50          -35     1        -1        
4    NC  North     20          -35    -1         1  offset
7    NC   West     15          -35    -1         1       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM