Originally published on Hackernoon
If you are a developer and want to integrate data manipulation or science into your product or starting your journey in data science, here are the Python libraries you need to know.
The goal of this series is to provide introductions, highlights, and demonstrations of how to use the must-have libraries so you can pick what to explore more in depth.
This library is built on top of NumPy, which you may remember from my last article. Pandas takes NumPy’s powerful mathematical array-magic one step further. It allows you to store & manipulate data in a relational table structure.
Focus of the Library
This library focuses on two objects: the Series (1D) and the DataFrame (2D). Each allow you to set:
- an index — that lets you find and manipulate certain rows
- column names — that lets you find and manipulate certain columns
Having SQL deja-vu yet?
Open a command line and type in
Windows: in the past I have found installing NumPy & other scientific packages to be a headache, so I encourage all you Windows users to download Anaconda’s distribution of Pythonwhich already comes with all the mathematical and scientific libraries installed.
A pandas data structure differs from a NumPy array in a couple of ways:
- All data in a NumPy array must be of the same data type, a pandas data structure can hold multiple data types
- A pandas data structure allows you to name rows and columns
- NumPy arrays can reach multiple dimensions, pandas data structures limit you to just 1 & 2D.*
*there is a 3D pandas data structure called a Panelbut it is depreciated
Let’s dive in!
It’s very simple!
You can create a Series or DataFrame from a list, tuple, NumPy array, or even a dictionary! Oh and of course from CSVs and databases.
The print out you see above has two columns. The one on the left is the index and the one on the right is your data. This index looks like the indexes we are used to when using lists, tuples, arrays, or any other iterable. We will see soon in pandas we can change it to whatever we like!
The print out you see above has a ton of numbers. The first column on the left is the index. The top row is the columns names (for now 0…5). Again, we will see soon in pandas we can change it to whatever we like!
From a Dictionary
The dictionary keys will become the index in a Series
It works a bit differently in a DataFrame — the keys become the column names
Pandas has many ways to upload data, but let’s focus on the standard csv format.
The keyword argument, index_col, is where you can specify which column in your CSV should be the index in the DataFrame. For more details on the read_csv function, go here.
I love that the pandas library only requires 1 line to import data from a CSV. Who else is over copying and pasting the same lines of code from the csv library? 😉
Use the Index
Your days of text wrangling are over! No more weird list comprehensions or for loops with comments like “# extract this column during given period” or “# sorry for the mess”.
Here is an example DataFrame:
Indexing a Column
Indexing a Row
Indexing multiple axes – names
Indexing multiple axes – numbers
View Your Data
Quickly check the top and bottom rows:
View summary statistics before you dash off for a meeting:
Control Your Data
Pandas brings the flexibility of SQL into Python.
Here are new example DataFrames:
If you want to join on a column other than the index, check out the merge method.
Notice how I was able to just add in a column using a key/value notation in the code above? Pandas allows you to add new data with ease. But it also allows you to access the core attributes of your data structures.
Access the Index
Access the Values
Access the Columns
I’m providing here a link to download my pandas walkthrough using a Jupyter Notebook!
Never used Jupyter notebooks before? Visit their website here.
Overall, if you have a dataset you want to manipulate but don’t want to go to the hassle of hauling it all into SQL, I recommend searching for a pandas solution before anything else!
Let’s look at a scenario. Say you wanted to keep an eye on Bitcoinbut don’t want to invest too much time in building out an infrastructure. You can use pandas to keep it simple.
This is the power of pandas with real life data! However, what if we wanted to view the data shown above in a graph? That’s possible, check out my next article on Matplotlib.
Thanks for reading! If you have questions feel free to comment & I will try to get back to you.
Check out my essentials list on Amazon