Sunday, 23 July 2017

Data Science & Machine Learning - 4.6 Pandas DataFrames Methods

Hi friends,

In the previous post under Data Science & Machine Learning, we discussed SQL functionalities of groupby, join and so on supported by Pandas DataFrames. In this post, we will see some important methods supported by the Pandas library that can be performed on DataFrames.

Note: All the commands discussed below are run in the Jupyter Notebook environment. See this post on Jupyter Notebook to know about it in detail. 

Pandas DataFrames Methods

Let's create a sample DataFrame to work with.


Here, I have created a sample DataFrame about employee details. 

So, let's now discuss the important methods supported by the Pandas library. 
  1. head() - Returns only the top few elements of the DataFrame to get an idea of how the data in the DataFrame looks like


  2. unique() - Returns the array of unique elements in a particular DataFrame's column


  3. nunique() - Returns the number of unique elements in a particular DataFrame's column


  4. value_counts() - Another very  important method that counts the occurrence of each element of a particular DataFrame column


  5. apply() - Although we do have methods such as min, max, etc. supported by Pandas to work with DataFrames yet what if we want to apply custom methods to DataFrames? That's when the apply methods comes to the rescue. The apply method helps us apply our own user defined methods to a DataFrames' columns. So, let's see an example of the apply method:


  6. In the above example, suppose we wanted to double the CTC of each employee of the DataFrame. For that, we have first defined our user defined method double and later use it on the CTC column using the apply method.

    We can achieve the above result in a single step by using lambda expression as shown below:


  7. columns() - Returns the list of column names of the DataFrame


  8. sort_values() - Sorts the DataFrame based on a specific column


  9. We can also sort the DataFrame in descending order by setting the ascending parameter to False.


We end this post here but you can refer this link to get the complete list of methods supported by Pandas library. In the next post, we'll see the various ways to input and output data to and from the Pandas DataFrames.
Share:

0 comments:

Post a Comment

Contact Me

Name

Email *

Message *

Popular Posts

Blog Archive