Question #129   Submitted by Answiki on 08/28/2020 at 09:30:09 AM UTC

In Python, how to sort a Pandas dataframe by column ?

Answer   Submitted by Answiki on 08/28/2020 at 09:43:50 AM UTC

In Python, you can sort a Pandas dataframe by a given column with the .sort_values() method. Let's consider the following dataframe:

	col1	col2	col3
0	Beta	1		4
1	Alpha	2		7
2	Charly	3		4


The following command sorts the dataframe by column col1 (ascending):

>>> df.sort_values(['col1'])

	col1	col2	col3
1	Alpha	2		7
0	Beta	1		4
2	Charly	3		4


The following command sorts the dataframe by column col2 (descending):

>>> df.sort_values(['col2'], ascending = False)

	col1	col2	col3
2	Charly	3		4
1	Alpha	2		7
0	Beta	1		4


The following command sorts the dataframe by column col3, then col2 (ascending):

>>> df.sort_values(['col3', 'col2'])

	col1	col2	col3
0	Beta	1		4
2	Charly	3		4
1	Alpha	2		7


5 events in history
Answer by Answiki on 08/28/2020 at 09:43:50 AM

In Python, you can sort a Pandas dataframe by a given column with the .sort_values() method. Let's consider the following dataframe:

	col1	col2	col3
0	Beta	1		4
1	Alpha	2		7
2	Charly	3		4


The following command sorts the dataframe by column col1 (ascending):

>>> df.sort_values(['col1'])

	col1	col2	col3
1	Alpha	2		7
0	Beta	1		4
2	Charly	3		4


The following command sorts the dataframe by column col2 (descending):

>>> df.sort_values(['col2'], ascending = False)

	col1	col2	col3
2	Charly	3		4
1	Alpha	2		7
0	Beta	1		4


The following command sorts the dataframe by column col3, then col2 (ascending):

>>> df.sort_values(['col3', 'col2'])

	col1	col2	col3
0	Beta	1		4
2	Charly	3		4
1	Alpha	2		7


Answer by Answiki on 08/28/2020 at 09:43:35 AM

In Python, you can sort a Pandas dataframe by a given column with the .sort_values() method. Let's consider the following dataframe:

	col1	col2	col3
0	Beta	1		4
1	Alpha	2		7
2	Charly	3		4

The following command sorts the dataframe by column col1 (ascending):

>>> df.sort_values(['col1'])

	col1	col2	col3
1	Alpha	2		7
0	Beta	1		4
2	Charly	3		4

The following command sorts the dataframe by column col2 (descending):

>>> df.sort_values(['col2'], ascending = False)

	col1	col2	col3
2	Charly	3		4
1	Alpha	2		7
0	Beta	1		4

The following command sorts the dataframe by column col3, then col2 (ascending):

>>> df.sort_values(['col3', 'col2'])

	col1	col2	col3
0	Beta	1		4
2	Charly	3		4
1	Alpha	2		7


Answer by Answiki on 08/28/2020 at 09:43:20 AM

In Python, you can sort a Pandas dataframe by a given column with the .sort_values() method. Let's consider the following dataframe:

	col1	col2	col3
0	Beta	1		4
1	Alpha	2		7
2	Charly	3		4

The following command sorts the dataframe by column col1 (ascending):

>>> df.sort_values(['col1'])

	col1	col2	col3
1	Alpha	2		7
0	Beta	1		4
2	Charly	3		4

The following command sorts the dataframe by column col2 (descending):

>>> df.sort_values(['col2'], ascending = False)

	col1	col2	col3
2	Charly	3		4
1	Alpha	2		7
0	Beta	1		4

The following command sorts the dataframe by column col3, then col2 (ascending):

>>> df.sort_values(['col3', 'col2'])

	col1	col2	col3
0	Beta	1		4
2	Charly	3		4
1	Alpha	2		7


Answer by Answiki on 08/28/2020 at 09:40:51 AM

In Python, you can sort a Pandas dataframe by a given column with the .sort_values() method. Let's consider the following dataframe:

	col1	col2	col3
0	Alpha	1		4
1	Beta	2		7
2	Charly	3		4

The following command sorts the dataframe by column col1 (ascending):

df.sort_values(['col1'])

The following command sorts the dataframe by column col2 (descending):

df.sort_values(['col1'], ascending = False)

The following command sorts the dataframe by column col3, then col2 (ascending):

df.sort_values(['col3', 'col2'])


Question by Answiki 08/28/2020 at 09:30:09 AM
In Python, how to sort a Pandas dataframe by column ?
# ID Query URL Count

Icons proudly provided by Friconix.