Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between dictionary and pandas series in Python

I have a requirement to keep data in key value pairs. I search and found 2 ways in python:

  1. default data structure dictionary.

    x = {'key':value}
    value = x['key']
    
  2. series of pandas data structure.

    x = pandas.Series({'key':value})
    value = x.key
    

I want to know the difference between this two apart from syntax.

like image 235
avni hirpara Avatar asked Apr 26 '17 13:04

avni hirpara


People also ask

Is a pandas series like a dictionary?

Pandas Series is a one-dimensional array of indexed data. It can be created using a list or an array. Pandas Series can be thought of as a special case of Python dictionary. It is a structure which maps typed keys to a set of typed values.

What is difference between pandas series and pandas DataFrame?

Series can only contain single list with index, whereas dataframe can be made of more than one series or we can say that a dataframe is a collection of series that can be used to analyse the data.

What is the difference between pandas series and Python lists?

A Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). It has to be remembered that unlike Python lists, a Series will always contain data of the same type.


2 Answers

Always read the docs first
But since you asked:

  • Dictionaries are one of python's default data structures which allow you to store key: value pairs and offer some built-in methods to manipulate your data, which you can read on the docs (here is a good summary to jump start your reading process).
  • Panda's Series are one-dimensional ndarrays with axis-labels, which allow you to store array-like, dict, or scalar values and are one of numpy's (a scientific computing python library) built-in data structures.
    If you read the docs provided above (see: Panda's Series link) you will notice that they come with a vast amount of methods and attributes quite different, for the most part, from those of a python dictionary.

So it is not just a syntax difference to say the least.

If you only need to store some key:value pairs, your best and more elegant solution is to use the default dictionary. If you need to make some complex data manipulation on the stored data, then consider using panda's series.

like image 117
John Moutafis Avatar answered Sep 21 '22 01:09

John Moutafis


There are 2 important differences.

1) Syntax and associated methods Allows for complex data manipulation in Panda series that would be difficult to achieve using a standard dictionary.

2) Order Standard python dictionaries are unordered sets; values can only be accessed by keys. Data in Panda series can be accessed by keys BUT can also be accessed with a numeric index because they are ordered.

In some ways, Panda series combine the best worlds of standard lists and standard dictionaries in python, but then top it off with some great data manipulation methods.

like image 28
maxalmond Avatar answered Sep 17 '22 01:09

maxalmond