Key Programming Concepts for Pandas Beginners | by Skyler Dale | Towards Data Science

If you’re just getting started with Python or Pandas, welcome.
Let me begin with a quick note of encouragement.
Learning to code can feel a bit daunting in the beginning because there is a lot of unknown.
On top of that, sometimes the people around you are discouraging: you don’t have the right background... if you don’t know x you can’t do y... the only proper way to get started with technology is to get a degree.
They’re wrong.
As is the case with any discipline, programming is a skillset that exists on a spectrum. Don’t buy in to any circular reasoning that suggests you can’t write code if you don’t know how to write code. The only thing you need is persistence.
You belong.
What is true is that there is a lot to learn. It’s not always going to be easy, and it takes continuous effort to move along that skillset spectrum.
In this post, I’ll provide an overview of some of the fundamental programming concepts so that when you start writing Pandas code, you have a better sense of what is going on.
I’ll try to bring each concept back to the Pandas library in order to make this as tangible as possible.
Put simply, a programming language is a set of rules and conventions that govern how we can instruct a computer to do what we want it to do.
Different programming languages use a different vocabulary or “syntax”.
In python, we can get the computer to return the words “hello world” by writing:
print(“hello world”)
In Java (another programming language) we write:
System.out.println("hello world");
Behind the scenes, programming languages are converted from high-level words, like “print”, all the way down to 1s and 0s.
Our computers can “understand” 1s and 0s because we can represent those values in a physical state.
For example, we could use a light switch. When it's on we could say it represents the number 1 and when it’s off it represents 0.
Computers have billions of tiny switches, called transistors, that work in the same way. Combined together, they encode all of the data you need.
A “high level” language is further away from those 1s and 0s, which means there is more translation required to get from the language to the state of the hardware.
A “low level” language is closer to the 1s and 0s and requires less translation.
There is a natural tradeoff here — high level languages tend to be slower but easier to use. Low level languages tend to be faster but harder to use.
A library (aka package), on the other hand, is code that someone else has written in a programming language and then tied up neatly so that other programmers can borrow it.
Python is a programming language. Pandas is an open source (ie. free, publicly available) data analysis library written in python.
One of the reasons why Python is such a popular choice for data analysis is because it’s community of developers has created a wide array of useful libraries including Pandas, Numpy, Matplotlib, Scikit-learn and many more.
When we want to leverage one of these libraries, we need to install the code (once) and import it into our files (each time we want to use it).
At its core, programming is all about transforming data; converting an input to an output.
But not all data is the same. We have different data types that we can use in different ways.
To further illustrate this point lets compare two of the most commonly used data types — a string (aka text) and an integer.
Integers can be divided. Strings cannot.
4/2=2. But “hello” / “goodbye” doesn’t mean anything.
Conversely, you can capitalize a string, but not an integer.
Thus, the major thing that distinguishes data types from one another is the functionalities that are associated with them.
In object oriented languages like Python, we can create our own data types, which are commonly referred to as classes.
A class is a blueprint that tells us what we can do with our data types (methods) and what information we can store about them (attributes).
A single instance of a class is called an object.
Drawing from our example above, we can run the capitalize method on a string:
But not an int:
Python has some built in data types (classes) like int, string, boolean, list, etc.
Pandas also has its own classes that make it possible to do data analysis work.
The most prevalent class in Pandas is the DataFrame, which stores tabular data (like an excel spreadsheet).
The developers that designed the DataFrame class wrote the code to give it the functionality that it has.
For example, we can use the “to_csv” method to create a csv file with the data in our DataFrame:
dataframe.to_csv(‘my_df.csv’)
Again, this functionality is tied to the DataFrame class, which is a part of the Pandas library.
In layman’s term: someone else wrote a lot of code so that you don’t have to.
This is what is meant by the term abstraction — and it's one of the most fundamentally awe-inspiring things about programming.
An army of other people has written millions of lines of code to go from the 1s and 0s, to the python language, to the Pandas library, to the DataFrame class, to the to_csv method.
But all you have to do is write one line of code.
A variable is a container that stores a value.
In some languages, you are required to assign a particular data type to a variable (these are called statically typed languages).
In other languages, you can store data in variables without assigning a particular type (dynamically typed languages).
Python is dynamically typed. As such, we can directly store a value inside a variable:
my_variable = 5
Java is statically typed, so if we want to put the integer, 5, directly in the variable we have to declare that the variable is a container for an integer and not any other data type:
Int myVariable = 10
Our variables can “store” any class in python.
A function is a process for converting inputs to outputs.
If that sounds familiar, it should.
We’ve already discussed “methods” which are a type of function. All methods are functions, not all functions are methods.
The key difference is that methods are tied to specific data types/classes, while general functions are not.
If that’s confusing, an example should clear things up.
When we want to capitalize a string, we can call the capitalize method. This method is explicitly tied to strings.
On the other hand, we can create our own functions that are not tied to a specific class.
Let’s say we want to write a function that adds the number 5 to the end of a string:
Now we have some functionality that is not called by an object, but rather called with a user-defined input (input_string).
In theory, strings could have this method. But it would be ridiculous to expect python to account for this arbitrary task in their definition of the string class.
In a real sense, then, functions help us create reusable code that is not already accounted for by the language or libraries we are using.
One of the most common mistakes that Pandas users make is not taking advantage of functions when they perform analytical tasks with their data.
What this leads to is messy and repetitive code.
Messy and repetitive code is more error prone and difficult to maintain.
It is best practice to use functions any time you might want to reuse the code you’re writing — both in practice and in theory.
Its also best practice to keep functions limited in scope so that they each perform a unique and isolated task.
Functions typically have inputs, which are called “parameters”. These are ingredients for the code of your function. They will typically be used in some way to produce an output.
Nothing happens until a user (usually yourself) runs the function by passing in values to take the place of those parameters.
The values you pass in are technically called “arguments”.
There’s a lot of words to keep track of here, so let’s close with some example code, which covers most of the concepts discussed above:
In this post, we went through some of the most important programming concepts and how they apply to the Pandas library.
If you want to learn more about how to get started with Pandas, check out some of my intuitive guides and make sure to subscribe below. Happy coding!