. Advertisement .
..3..
. Advertisement .
..4..
Sorting data Pandas DataFrame is the basic operation when you have to work with data. This will help you save time as well as easily filter data. If you still don’t know the steps, follow our article “How to Sort Pandas DataFrame“.
Use the sort_values
To sort pandas dataframe data, you would use the sort_values function.
The sort_values() function allows you to sort the Dataframe data by the specified label. The returned data results can be sorted in ascending or descending order. Through the boolean property you can adjust to sort in the order you want (ascending/descending)
- Syntax:
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last', ignore_index=False, key=None)
- Detail:
by
– data column/row nameaxis
– The axis to be sorted, default is 0ascending
– Conditions apply to sort in ascending or descending order. If not required, it will default to ascending.inplace
– Set default to false. If correct, the result will be updated.kind
– Sort by {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, default ‘quicksort’na_position-
Default is “last”. Specifies where to store {‘first’, ‘last’}ignore_index
– index parameter.key-
optional
How to Sort Pandas DataFrame
From the above syntax, you can apply to sort the dataframe in ascending/descending/by column data. To make it easier for you to visualize, here is an example we give.
Here is the data you need to process (sorted):
Product | Quantity | Price |
rose | 700 | 9 |
tulip | 800 | 7 |
peony | 1000 | 12 |
lily | 1200 | 12 |
As a first step, you need to create a Dataframe, something like this:
import pandas as pd
data = {'Product': ['rose','tulip','peony','lily'],
'Quantity': [700,800,1000,1200],
'Price': [9,7,12,12]
}
df = pd.DataFrame(data, columns=['Product','Quantity','Price'])
print (df)
Here are ways to sort Dataframe:
1. Sort in ascending order
According to the above, the sort_values function defaults to sorting in ascending order. If you want to sort the displayed “product” column in ascending order, we have the following program:
import pandas as pd
data = {'Product': ['rose','tulip','peony','lily'],
'Quantity': [700,800,1000,1200],
'Price': [9,7,12,12]
}
df = pd.DataFrame(data, columns=['Product','Quantity','Price'])
# sort Product in an ascending order
df.sort_values(by=['Price'], inplace=True)
print (df)
Output:
Product Quantity Price
1 tulip 900 7
0 rose 700 9
2 peony 1000 12
3 lily 1200 12
2. Sort in descending order
In case you want to sort in descending order, when declaring you just need to adjust ascending=False, we have the following:
import pandas as pd
data = {'Product': ['rose','tulip','peony','lily'],
'Quantity': [700,800,1000,1200],
'Price': [9,7,12,12]
}
df = pd.DataFrame(data, columns=['Product','Quantity','Price'])
# sort Brand in a descending order
df.sort_values(by=['Price'], inplace=True, ascending=False)
print (df)
Output:
Product Quantity Price
3 lily 1200 12
2 peony 1000 12
0 rose 700 9
1 tulip 800 7
3. Sort by columns
If you want to sort the data by two parameters, for example “price” and “quantity” we will program like this:
import pandas as pd
data = {'Product': ['rose','tulip','peony','lily'],
'Quantity': [700,800,1000,1200],
'Price': [9,7,12,12]
}
df = pd.DataFrame(data, columns=['Product','Quantity','Price'])
# sort by multiple columns: Quantity and Price
df.sort_values(by=['Quantity','Price'], inplace=True)
print (df)
Output:
Product Quantity Price
1 tulip 800 7
0 rose 700 9
2 peony 1000 12
3 lily 1200 12
According to the table above, price and quantity are being sorted in ascending order.
Conclusion
Thus, we have solved for you the questions “How to Sort Pandas DataFrame“. If you have any comments, please contact us immediately for answers. Thanks for reading!
Leave a comment