How to Sort Data in Pandas | #17 of 53: The Complete Pandas Course

Опубликовано: 06 Май 2022
на канале: Selva Prabhakaran (ML+)
1,013
7

Course materials Github: https://github.com/machinelearningplu...

Join Pandas course on ML+: https://edu.machinelearningplus.com/c...
--------------------

Sorting is another very essential data manipulation or data wrangling activity is very common.

You can sort a data frame based on one single column you can sorted based on multiple columns and a mix of both ascending and descending order that is one column you are sorting it in ascending fashion, another column you are sorting in descending fashion, all of these are possible. And all of these are possible using one single function called sort underscore values, we are going to use the same churn Data Set Data Set looks something like this.

Now, from this data set, if we want to get say the top five customers who had the lowest number of say the total day calls right, this particular column, you want to see which are all the different customers who have made the lowest number of total day calls, who are the top five customers to do that use df sort values, simply write the by argument alone, set it to that variable of interest, you will get the output so on doing this, it will return the entire data frame.

And if you take the first five rows, all these five rows, all these rows are the customers that we are looking at five customers who have made the lowest total day calls All right, what is descending SOT you just need to add one more argument set ascending equal to false.

For example, if you want to get five customers who have made the highest number of day calls to do that, I need to still change this parameter total because run this the same data frame is no sorted in descending order total bagels the highest values appear at the top.

The third use case is a mix of both ascending and descending sort in one column, you want to sorted in ascending fashion in another column you want to sort it in descending fashion, say the state you want it to be ascending total day calls you want it to be in descending to do that, instead of passing just false to your ascending parameter if you said yes false here both of these will be sorted in descending order

Alright, if you set a list of false and true values in the same order in which the columns are occurring in the BI argument, right, these will be mapped these will be mapped. So state will be sorted in ascending order which is true, total day Gods will be sorted in descending order since this is false. Let's run this and see.

So state this is an ascending order right is the starting with a and the last date why this is starting in w likewise, within the states that start with a K, the total De Gaulle's is sorted in descending order. So these are the different scenarios you will typically use sorting for