Pandas Sort DataFrame by Single Column

You can sort pandas DataFrame by single column using sort_values() method and by ascending or descending order. To specify the order, you have to use ascending boolean property; False for descending and True for ascending. By default, it is set to True. In this article, I will explain how to sort pandas DataFrame by single column using DataFrame.sort_values() method and examples.

1. Quick Examples of Sort DataFrame by Single Column

If you are in a hurry, below are some quick examples of how to sort DataFrame by single column.


# Below are quick example

# Sort single column
df2 = df.sort_values('Fee')

# Sort single column using inplace
df.sort_values("Fee", inplace=True)

# Sort descending order using DataFrame.sort_values()
df2 = df.sort_values(by=['Fee'], ascending=False)

# Sort descending order
df.sort_values(by='Fee', inplace=True, ascending=False)

# Sort ascending order
df2 = df.sort_values(by=['Fee'], ascending=True)

Let’s create a DataFrame with a few rows and columns and execute some examples to learn how sort single column. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


import pandas as pd
technologies = ({
    'Courses':["Spark","Hadoop","pandas","Java","Pyspark"],
    'Fee' :[20000,25000,30000,22000,26000],
    'Duration':['30days','40days','35days','60days','50days'],
    'Discount':[1000,2500,1500,1200,3000]
               })
df = pd.DataFrame(technologies, index = ['r1','r2','r3','r4','r5'])
print(df)

Yields below output.


    Courses    Fee Duration  Discount
r1    Spark  20000   30days      1000
r2   Hadoop  25000   40days      2500
r3   pandas  30000   35days      1500
r4     Java  22000   60days      1200
r5  Pyspark  26000   50days      3000

2. Use DataFrame.sort_values() to Sort Single Column

By using the DataFrame.sort_values() method you can sort a single column in pandas DataFrame by ascending or descending order. When not specified order, for instance df.sort_values('Fee') sorts Fee column by ascending order.


# Sort single column
df2 = df.sort_values('Fee')
print(df2)

Yields below output.


    Courses    Fee Duration  Discount
r1    Spark  20000   30days      1000
r4     Java  22000   60days      1200
r2   Hadoop  25000   40days      2500
r5  Pyspark  26000   50days      3000
r3   pandas  30000   35days      1500

In case if you wanted to update the existing DataFrame use inplace=True.


# Sort single column using inplace
df.sort_values("Fee", inplace=True)
print(df)

Yields same output as above.

3. Use DataFrame.sort_values() to Sort Descending Order

In case if you wanted to sort by descending order, use ascending=False. You can also specify different sorting orders for each column.


# Sort descending order using DataFrame.sort_values()
df2 = df.sort_values(by=['Fee'], ascending=False)
print(df2)

# Sort descending order
df.sort_values(by='Fee', inplace=True, ascending=False)
print(df)

Yields below output.


    Courses    Fee Duration  Discount
r3   pandas  30000   35days      1500
r5  Pyspark  26000   50days      3000
r2   Hadoop  25000   40days      2500
r4     Java  22000   60days      1200
r1    Spark  20000   30days      1000

4. Complete Example For Sort DataFrame by single Column


import pandas as pd
technologies = ({
    'Courses':["Spark","Hadoop","pandas","Java","Pyspark"],
    'Fee' :[20000,25000,30000,22000,26000],
    'Duration':['30days','40days','35days','60days','50days'],
    'Discount':[1000,2500,1500,1200,3000]
               })
df = pd.DataFrame(technologies, index = ['r1','r2','r3','r4','r5'])
print(df)

# Sort single column
df2 = df.sort_values('Fee')
print(df2)

# Sort single column using inplace
df.sort_values("Fee", inplace=True)
print(df)

# Sort descending order using DataFrame.sort_values()
df2 = df.sort_values(by=['Fee'], ascending=False)
print(df2)

# Sort descending order
df.sort_values(by='Fee', inplace=True, ascending=False)
print(df)

# Sort ascending order
df2 = df.sort_values(by=['Fee'], ascending=True)
print(df2)

Conclusion

In this article, you have learned how to sort pandas DataFrame by single column using Dataframe.sort_values() by ascending or descending order.

Happy Learning !!

You May Also Like

References

Leave a Reply

Pandas Sort DataFrame by Single Column