How do I sort a pandas DataFrame or a Series?




[ad_1]

pandas allows you to sort a DataFrame by one of its columns (known as a “Series”), and also allows you to sort a Series alone. The sorting API changed in pandas version 0.17, so in this video, I’ll demonstrate both the “old way” and the “new way” to sort. I’ll also show you how to sort a DataFrame by multiple columns at once!

SUBSCRIBE to learn data science with Python:
https://www.youtube.com/dataschool?sub_confirmation=1

JOIN the “Data School Insiders” community and receive exclusive rewards:
https://www.patreon.com/dataschool

== RESOURCES ==
GitHub repository for the series: https://github.com/justmarkham/pandas-videos
Changes to sorting API in pandas 0.17: http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#changes-to-sorting-api
DataFrame “sort_values” documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html
Series “sort_values” documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.sort_values.html
DataFrame “sort” documentation (deprecated): http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.sort.html
Series “order” documentation (deprecated): http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.Series.order.html

== LET’S CONNECT! ==
Newsletter: https://www.dataschool.io/subscribe/
Twitter: https://twitter.com/justmarkham
Facebook: https://www.facebook.com/DataScienceSchool/
LinkedIn: https://www.linkedin.com/in/justmarkham/

Source


[ad_2]

Comment List

  • Data School
    December 12, 2020

    Thanks a lot for these amazing tutorials.

  • Data School
    December 12, 2020

    Hi. I have a question. How do you sort the dataframe by genre –not necessarily in alphabetical or ascending order? Say I want all the Mystery movies first, then Biographies, then Drama and so on. Tia!

  • Data School
    December 12, 2020

    Awesome! Love the way you explain things – thanks

  • Data School
    December 12, 2020

    Just in one word. YOU ARE AWESOME!!!!! THANKS SO MUCH !!!

  • Data School
    December 12, 2020

    You're amazing.

  • Data School
    December 12, 2020

    Anyone has a clue why I can see all the rows like shown in the video? I mean dot head say 100 doesn't do jack to the number of rows displayed.
    Appreciate the work you put into these videos Kevin. Thank you!

  • Data School
    December 12, 2020

    Sir while doing sort or doing another things with the data sets it is only showing the data set from 0 to 4 and … Then 974 and some number

  • Data School
    December 12, 2020

    Thanks to Covid I'm taking the time to learn a new skill set and these videos are definitely helping me understand my assignments better. In fact, some of my assignments use the same datasets! Thanks so much for making them, what I miss or don't understand in class is offered here from another perspective <3 🙂

  • Data School
    December 12, 2020

    Kelvin, appreciate a lot! A comment from 2020, meet this amazing video from a sad year.

  • Data School
    December 12, 2020

    how do i read a binary file in pandas

  • Data School
    December 12, 2020

    Hello Kevin, first of all your video's are a great help to learn Python so thank you. But sometimes I get an error and I have nog clue how to fix it. Is it possible to make a video about "error handling/solving" ?

  • Data School
    December 12, 2020

    Hi, just want to know at 3:43, when we are sorting, Why we have written ascending=False?

  • Data School
    December 12, 2020

    How can I change the CRS of a DataFrame with out Warning?

  • Data School
    December 12, 2020

    Please guide which python library is excellent for data analysis, data wrangling, financial and accounting calculations and data manipulation. As I am a Professional Accountants and works a lot on advanced excel, power bi. Kindly do advise on this. And secondly, while reading file, I couldn't get data in frame (means with line borders across rows and columns). Please do assist. Thank you, awaiting your response.

  • Data School
    December 12, 2020

    Thank you so much. Videos are very helpful and easy to understand.

  • Data School
    December 12, 2020

    movies.sort_values(['star_rating','duration'] , ascending=[False,True],inplace=True)

    movies[:20]
    watch these 20 movies

  • Data School
    December 12, 2020

    Thank you very much,

    explain very well and clearly

  • Data School
    December 12, 2020

    sort is not working in my system, why?

  • Data School
    December 12, 2020

    When using sort, numbers are given priority. Which order the numbers are arranged ? They seem to be in random order.
    Second, which order the first alphabets (after the nos.) are arranged. They are also random?

    Thanking you for the videos .

  • Data School
    December 12, 2020

    Hi Kevin, this is first series of ur channel that I'm watching, it is absolutely useful n informative, u hv taken evn minute details that we normally tend to skip, one more thing pls make video series on numpy and ML explain all models (if possible)… Thank you so much for these wonderful video… God bless!

  • Data School
    December 12, 2020

    What if i want to find the longest en shittiest movie? When trying to do: movies.sort_values(['star_rating', 'duration'], ascending=False) i will only get the best longest movie.

  • Data School
    December 12, 2020

    Amazing explanation. Clear, structured presentation from a highly experienced human 10/10

  • Data School
    December 12, 2020

    if you want to see what each column has as an "unique" value, you could use movies["content_rating"].unique(), you can get a sense about how the sorting is going to be.

  • Data School
    December 12, 2020

    Thanks a lot! You saved my life!

  • Data School
    December 12, 2020

    how do we check pandas version

Write a comment