Ben Chuanlong Du's Blog

It is never too late to learn.

Convert Between a Pandas.Series and a Dict-Like Object

In [1]:
import pandas as pd

Series to dict

In [4]:
s = pd.Series(["how", "are", "you"])
s
Out[4]:
0    how
1    are
2    you
dtype: object
In [5]:
s.to_dict()
Out[5]:
{0: 'how', 1: 'are', 2: 'you'}

dict to Series

In [8]:
dic = {"how": 0, "are": 2, "you": 1}
dic
Out[8]:
{'how': 0, 'are': 2, 'you': 1}
In [9]:
pd.Series(dic)
Out[9]:
how    0
are    2
you    1
dtype: int64
In [ ]:

Comments