Pandas sort column values with "0" values at end of column

Question:

I have a dataframe which looks like this

pd.DataFrame({'A': [5, 2, 0, 0, -3, -2, 1]}).sort_values('A')
Out[6]: 
   A
4 -3
5 -2
2  0
3  0
6  1
1  2
0  5

I would like to have "0" values at the end when sorting so my resulting dataframe looks like this.

   A
4 -3
5 -2
6  1
1  2
0  5
2  0
3  0

Is there a simple (1 line of code) solution?

Asked By: idt_tt

||

Answers:

Let’s try adding a new column and sort by two columns:

df.assign(dummy=df.A.eq(0)).sort_values(['dummy','A']).drop('dummy', axis=1)

Another option, not quite a one-liner, is mask and concat:

mask = df['A'].eq(0)
df = pd.concat([df[~mask].sort_values('A'), df[mask]])

Output:

   A
4 -3
5 -2
6  1
1  2
0  5
2  0
3  0
Answered By: Quang Hoang

First mask the zeros then use argsort on column A to get the indices that would sort the dataframe:

df.iloc[df['A'].replace(0, np.nan).to_numpy().argsort()]

   A
4 -3
5 -2
6  1
1  2
0  5
2  0
3  0
Answered By: Shubham Sharma

Another option is to use key in sort_values()

df.sort_values(by='A', key=lambda col: col.replace(0, np.nan), na_position='last')

Answered By: benr
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.