Pandas Concepts: Reference and Examples

Pandas Concepts: Reference and Examples

Last updated:
Table of Contents

Index

A Index is just a Numpy array.

An Index is used to locate elements in a Series or rows in a DataFrame.

import pandas as pd

i = pd.Index([0,1,2,3])
# Int64Index([0, 1, 2, 3], dtype='int64')

An Index may contain non-numeric elements

import pandas as pd

string_i = pd.Index(['a','b','c','d'])
string_i
#Index(['a', 'b', 'c', 'd'], dtype='object')

Series

A Series is a data ndarray plus an Index.

import pandas as pd

i = pd.Index([0,1,2,3])
# Int64Index([0, 1, 2, 3], dtype='int64')

s = pd.Series([10,20,30,40],index=i)
# 0    10
# 1    20
# 2    30
# 3    40
# dtype: int64

Dataframe

A dataframe is a list of Series objects, where all Series have the same index.

Index vs Series

The Index is part of a Series.

Series = Index + Data array

Series vs Dataframe

A dataframe is a list of Series objects, where all Series have the same index.

You can turn a Series into a DataFrame (with a single column) by calling .toframe() on the Series object.