Using pandas in Python

142 VIEWS

·

pandas is an open source data analysis package developed for Python. It is designed to be easy to use, efficient, and convenient for real-world, practical data analysis. For this reason, it is one of the more powerful and widely used tools amongst data scientists. The library can be found here, where you will also find additional documentation and installation instructions.

pandas is designed for tabular datasets (similar to those used in SQL or Excel), that contain observational data. It makes cleaning data and extracting statistical significance relatively easy. Additionally, pandas allows you to merge, filter, group, order, and join with simple, intuitive syntax. This article is intended to be an introduction to using the pandas library, where I will demonstrate several of these capabilities, and their relative simplicity. Before starting, I recommend downloading the pandas cheat sheet that can be found here. It contains most of the basic functions and the corresponding syntax. As a data scientist, this makes your life a lot easier, as there is no need to memorize everything. As you become more familiar with pandas, the intuitive structure will become more apparent.

Exploring Data

First off, we’ll import some data. The UCI Machine Learning Repository has a myriad of datasets ready to use. The wine dataset is what we will be using today. It contains 178 observations of wine grown in the same region in Italy. Each observation is from one of three cultivars (the ‘Class’ feature), and also has 13 constituent features that are the result of a chemical analysis.

In this first chunk, we’ll import the pandas library and the wine dataset. We’ll then convert the dataset in a pandas DataFrame. There are two primary types of data structures in pandas: a Series (one-dimensional) and a DataFrame (two-dimensional). Nearly all datasets can utilize these two data structures. One of the more useful methods is .head(n). This concatenates the output to the first n observations, allowing you to view any number of observations you desire. You can also use .sample(n), which will output n random observations.

Some other useful functions:

The .describe() method will output quick and basic statistical information on all of the features within the DataFrame. This is quite useful when dealing with numerical data.

Manipulating Data

One of the more efficient features of pandas is called method chaining. For users that are familiar with R, this is similar to the pipe operator %>%. This allows you to chain methods together. For example, if you wanted to find the mean values of each feature for each of the 3 cultivars, you would first use groupby(‘Class’), then .mean(). This can be done all in one line.


You can then chain additional methods, such as .plot()


pandas has built-in plotting features that can assist the user with the data exploration process. This is often useful with much larger datasets, where the physical significance of the features is unknown. With pandas, it is easy to sort through data (numerically or alphabetically) and rename the features.

There are a number of ways to extract specific features, either specifying the names directly or using the column index.

pandas also allows conditional-based extraction. Here, we extract observations that have alcohol levels above 14%, and we can view only the Ash and Alcalinity of ash features.

These are just a few of the basic capabilities of the pandas library. As a data scientist, they make the job of data wrangling a much simpler task. Capable of performing sophisticated grouping, filtering, and joining operations in single lines, pandas is a powerful tool for preparing data for a diverse set of machine learning algorithms.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Dante is a former physicist who left the laboratory to join the ranks of scientists and engineers turning to techniques within data science to solve today’s problems. He is currently pursuing a Master's degree in Data Science for Complex Economic Systems. He lives in Turin, Italy.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu