One of the most popular 3rd party Python packages is called pandas. The pandas package “is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language.” It is used by data scientists and software engineers around the world.
In this tutorial, you will learn a little about creating different kinds of empty or partially empty DataFrames with pandas. Then you will learn a couple of different ways to add data to that DataFrame.
Specifically, you will learn about the following:
- Creating an empty DataFrame and adding data
- Creating an empty DataFrame with columns and adding data
- Creating an empty DataFrame with columns and indices and adding data
Creating an Empty DataFrame in pandas
Sometimes you just need to create an empty DataFrame, much like you sometimes need to create an empty Python dictionary or list.
Here is an example of creating a completely empty DataFrame with pandas:
>>> import pandas as pd >>> df = pd.DataFrame() >>> df Empty DataFrame Columns: [] Index: []
Of course, an empty DataFrame isn’t especially useful. So let’s add a little data to the DataFrame!
>>> import pandas as pd >>> df = pd.DataFrame() >>> df Empty DataFrame Columns: [] Index: [] >>> df["Name"] = ["Mike", "Steve", "Rodrigo"] >>> df["Jobs"] = ["Engineer", "Core Dev", "Content Creator"] >>> df Name Jobs 0 Mike Engineer 1 Steve Core Dev 2 Rodrigo Content Creator
This example demonstrates how you can specify columns in pandas and add data to those columns.
Now let’s learn how to create an empty DataFrame that includes columns, but no data!
Creating an Empty DataFrame with Columns
This next example will show you how to create a pandas DataFrame that includes columns, but no indices or column data.
Let’s take a look:
>>> import pandas as pd >>> df = pd.DataFrame(columns=["Name", "Job"]) >>> df Empty DataFrame Columns: [Name, Job] Index: [] # Add some data using append() >>> df = df.append({"Name": "Mike", "Job": "Blogger"}, ignore_index=True) >>> df Name Job 0 Mike Blogger >>> df = df.append({"Name": "Luciano", "Job": "Author"}, ignore_index=True) >>> df Name Job 0 Mike Blogger 1 Luciano Author
Well, that was better than a completely empty DataFrame! In this example, you also learn how to use the DataFrame’s append() method to add data to each column.
When you use append() it takes in a dictionary of column name and value. You also set ignore_index to True, which lets pandas update the index for you automatically.
Now let’s look at how to create one more type of empty DataFrame with pandas!
Creating an Empty DataFrame with Columns and Indices
For this example, you will learn how to create a pandas DataFrame that has two columns and three named rows or indices.
Here’s how it is done:
>>> import pandas as pd >>> df = pd.DataFrame(columns = ["Name", "Job"], index = ["a", "b", "c"]) >>> df Name Job a NaN NaN b NaN NaN c NaN NaN
When you print out the DataFrame, you can see that the columns all contain NaN, which stands for “Not a Number”. A NaN is kind of like a None in Python.
One way to add data to this DataFrame in pandas is to use the loc attribute:
>>> df.loc["a"] = ["Mike", "Engineer"] >>> df Name Job a Mike Engineer b NaN NaN c NaN NaN >>> df.loc["b"] = ["Steve", "Core Dev"] >>> df Name Job a Mike Engineer b Steve Core Dev c NaN NaN >>> df.loc["c"] = ["Rodrigo", "Content Creator"] >>> df Name Job a Mike Engineer b Steve Core Dev c Rodrigo Content Creator
When you use the loc attribute, you use dictionary-like syntax to set a specific index to a list of values. The above example shows how to add three rows of data.
Wrapping Up
This tutorial doesn’t even begin to scratch the surface of what you can do with pandas. But it isn’t supposed to. That’s what books are for. Here you learned how to create three different types of empty DataFrames.
To be specific, you learned the following:
- Creating an empty DataFrame and adding data
- Creating an empty DataFrame with columns and adding data
- Creating an empty DataFrame with columns and indices and adding data
Hopefully, you will find these examples useful in your pandas journey. Happy coding!