What is the difference between Pandas series and Python lists?

pandas
python
series

#1

Are the Series in Pandas similar to Python lists or we do have a fundamental difference?

In case there is a difference, does explicitly converting a list into series is beneficial while creating a dataframe from a python list ?


#2

Hey @pulkitpahwa,

1. Series and Lists:

TL;DR Series is a 1D data structure designed for a particular use case which is quite different from a list. Yet they both are 1D, ordered data structures. Follow to know more :

The pandas documentation defines a Series as -

Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). The axis labels are collectively referred to as the index.

Series index vs List index
You might wonder that list is also one dimensional data type capable of holding any data type and have indices. What’s the difference? Look at this -


Series like a Dictionary
You can actually control the index(label) of elements. In this case the Series label can be thought of similar to python dictionary. Because you can also do this -

Series like a vector
Series also support vector operations. Any operation to perform on a series , get’s performed on every single element. Something like this -

In case of the list, it doubled the list with the same elements(typical behaviour). In case of the series though, it multiplied each element by 2. I could also do this -

series_var ** 3

24

2. Should you explicitly convert a list to a series to create a data frame from a list?

TL;DR : While a series only support a single dimension, data frames are 2 dimensional objects. The pandas.DataFrame(..) function has provisions for creating data frames from lists. Though we usually add a bit of dictionary magic in between but I haven’t seen any issue as such in using lists. Here are some examples -

Creating data frame from a dictionary of lists

You can easily create a data frame from a dictionary of lists. In this dictionary, each key will become your column name in the data frame and each value will become row values in that column.

Creating data frame from a list of dictionaries

You can also create a data frame from a list of dictionary objects. For example, I want to keep a count of ballons left in each packet based on colour

The missing values are dennoted by NaN.

Hope this helps,
Sanad :slight_smile: