Doing Data Analysis and Data Science in Python with pandas

Doing Data Analysis and Data Science in Python with pandas

Ever wonder how you can best analyze data in python? Wondering how you can advance your skills beyond doing basic analysis in tools like MS Excel or SAS? Want to learn how to do the data analysis in python and pandas? Then read this post.

pandas is a Python package providing fast, flexible, and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive. It aims to be the fundamental high-level building block for doing practical, real world data analysis in Python. Additionally, it has the broader goal of becoming the most powerful and flexible open source data analysis / manipulation tool available in any language. It is already well on its way toward this goal.

pandas is well suited for many different kinds of data:

  • Tabular data with heterogeneously-typed columns, as in an SQL table or Excel spreadsheet
  • Ordered and unordered (not necessarily fixed-frequency) time series data.
  • Arbitrary matrix data (homogeneously typed or heterogeneous) with row and column labels
  • Any other form of observational / statistical data sets. The data actually need not be labeled at all to be placed into a pandas data structure

The two primary data structures of pandas, Series (1-dimensional) and DataFrame (2-dimensional), handle the vast majority of typical use cases in finance, statistics, social science, and many areas of engineering. For R users, DataFrame provides everything that R’s data.frame provides and much more. pandas is built on top of NumPy and is intended to integrate well within a scientific computing environment with many other 3rd party libraries.

Here are just a few of the things that pandas does well:

  • Easy handling of missing data (represented as NaN) in floating point as well as non-floating point data
  • Size mutability: columns can be inserted and deleted from DataFrame and higher dimensional objects
  • Automatic and explicit data alignment: objects can be explicitly aligned to a set of labels, or the user can simply ignore the labels and let , , etc. automatically align the data for you in computations
  • Powerful, flexible group by functionality to perform split-apply-combine operations on data sets, for both aggregating and transforming data
  • Make it easy to convert ragged, differently-indexed data in other Python and NumPy data structures into DataFrame objects
  • Intelligent label-based slicing, fancy indexing, and subsetting of large data sets
  • Intuitive merging and joining data sets
  • Flexible reshaping and pivoting of data sets
  • Hierarchical labeling of axes (possible to have multiple labels per tick)
  • Robust IO tools for loading data from flat files (CSV and delimited), Excel files, databases, and saving / loading data from the ultrafast HDF5 format
  • Time series-specific functionality: date range generation and frequency conversion, moving window statistics, moving window linear regressions, date shifting and lagging, etc.

Many of these principles are here to address the shortcomings frequently experienced using other languages / scientific research environments. For data scientists, working with data is typically divided into multiple stages: munging and cleaning data, analyzing / modeling it, then organizing the results of the analysis into a form suitable for plotting or tabular display. pandas is the ideal tool for all of these tasks.

Some other notes

  • pandas is fast. Many of the low-level algorithmic bits have been extensively tweaked in Cython code. However, as with anything else generalization usually sacrifices performance. So if you focus on one feature for your application you may be able to create a faster specialized tool.
  • pandas is a dependency of statsmodels, making it an important part of the statistical computing ecosystem in Python.
  • pandas has been used extensively in production in financial applications.

Lessons for New pandas Users

  • 01 - Lesson: - Importing libraries - Creating data sets - Creating data frames - Reading from CSV - Exporting to CSV - Finding maximums - Plotting data
  • 02 - Lesson: - Reading from TXT - Exporting to TXT - Selecting top/bottom records - Descriptive statistics - Grouping/sorting data
  • 03 - Lesson: - Creating functions - Reading from EXCEL - Exporting to EXCEL - Outliers - Lambda functions - Slice and dice data
  • 04 - Lesson: - Adding/deleting columns - Index operations
  • 05 - Lesson: - Stack/Unstack/Transpose functions
  • 06 - Lesson: - GroupBy function
  • 07 - Lesson: - Ways to calculate outliers
  • 08 - Lesson: - Read from Microsoft SQL databases
  • 09 - Lesson: - Export to CSV/EXCEL/TXT
  • 10 - Lesson: - Converting between different kinds of formats
  • 11 - Lesson: - Combining data from various sources

<Note> This documentation assumes general familiarity with NumPy. If you haven’t used NumPy much or at all, do invest some time in learning about NumPy first.

For more resources, please visit the main repository.

pandas Cookbook

The goal of this cookbook (by Julia Evans) is to give you some concrete examples for getting started with pandas. These are examples with real-world data, and all the bugs and weirdness that that entails.

Here are links to the v0.1 release. For an up-to-date table of contents, see the pandas-cookbook GitHub repository. To run the examples in this tutorial, you’ll need to clone the GitHub repository and get IPython Notebook running. See How to use this cookbook.

  • A quick tour of the IPython Notebook: Shows off IPython’s awesome tab completion and magic functions.
  • Chapter 1: Reading your data into pandas is pretty much the easiest thing. Even when the encoding is wrong!
  • Chapter 2: It’s not totally obvious how to select data from a pandas dataframe. Here we explain the basics (how to take slices and get columns)
  • Chapter 3: Here we get into serious slicing and dicing and learn how to filter dataframes in complicated ways, really fast.
  • Chapter 4: Groupby/aggregate is seriously my favorite thing about pandas and I use it all the time. You should probably read this.
  • Chapter 5: Here you get to find out if it’s cold in Montreal in the winter (spoiler: yes). Web scraping with pandas is fun! Here we combine dataframes.
  • Chapter 6: Strings with pandas are great. It has all these vectorized string operations and they’re the best. We will turn a bunch of strings containing “Snow” into vectors of numbers in a trice.
  • Chapter 7: Cleaning up messy data is never a joy, but with pandas it’s easier.
  • Chapter 8: Parsing Unix timestamps is confusing at first but it turns out to be really easy.

The book can be downloaded here

Some Awesome Resources

<Note> All of the content, text, links, references in this post are taken from the pandas documentation website

Links and references do not represent wholesale endorsements of the resource or the author. This is not a comprehensive list,  so I welcome your suggestions and comments.

Best,

Ali Syed

I look forward to your comments below and @aaalee.

 

Muhammad Shahbaz Khan

Manager - Assurance (Risk & Controls) | Data Analytics enthusiast | All opinions here are personal

7y

R does the same as pandas in python !!!

Like
Reply
Kirk Mettler

Chief Data Scientist and R guy at IBM

8y

I heard that pandas is working on being able to use parquet format directly which could really speed things up for Hadoop users of Pandas.

A very quick guide lines of how to use and the usages of Python in Data Analytics. This will help lots of people like me who have just entered into this field and started learning. Great Ali ... Keep it up .

very powerful indeed

Like
Reply

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics