How to use Pandas DataFrame

1 min

Pandas is today one of the leader in the Data Science industry due to its state of the art Python data manipulation toolkit.

DataFrames are at the core of the Pandas library.

A DataFrame can be understood exactly as an excel spreadsheet.

With column and rows.

Installation and Import

First of all, we need to install Pandas using the pip, the python package manager.

pip install pandas
How to install Pandas for Python 2.7


pip3 install pandas
How to import pandas for Python 3.x
import pandas as pd
How to import pandas

DataFrame core elements

Each column in a pandas.DataFrame is a pandas.Series.

The anatomy of a DataFrame

And when you loop over the DataFrame each row can be understood as a Python dictionary.

How to create a DataFrame

At creation we can either pass a list as index and/or dictionary as data as argument.

The creation of a pandas.DataFrame is quite flexible.

At creation a DataFrame can be empty, with one index, without index but with data, with both an index and data.

# An empty dataframe
df = pd.DataFrame()

# A dataframe with an index only
df = pd.DataFrame(index=range(0,10))

# A dataframe with data only
df = pd.DataFrame(data={"col1": range(0,10)})

# A dataframe with both index and data
df = pd.DataFrame(index=range(0,10), data={"col1": range(0,10)})
The different ways of declaring a DataFrame

The columns data are passed as lists within the data dictionary.

If you want to learn more about DataFrame and how to make the most out of them I wrote a lot of articles that you might find useful.

DataFrame - The Python You Need
We gathered the only Python essentials that you will probably ever need.