Predictive Hacks

How to Transform a Pandas DataFrame into a Dictionary

This is one useful tip especialy when you want to return your dataframe as a JSON file when you are working with flask. There are 3 ways to do it.

import pandas as pd

df=pd.DataFrame(
{
'Gender':np.random.choice(["m","f"],10,p=[0.6,0.4]),
'Age':np.random.choice(["[<30]","[30-65]", "[65+]"],10,p=[0.3,0.6,0.1]),
    }
)
df
  Gender      Age
0      m  [30-65]
1      m  [30-65]
2      f  [30-65]
3      m  [30-65]
4      m    [<30]
5      m  [30-65]
6      m    [<30]
7      f  [30-65]
8      f  [30-65]
9      f    [<30]

Dict

This is the most known method.

df.to_dict()
{'Gender': {0: 'm',
  1: 'm',
  2: 'f',
  3: 'm',
  4: 'm',
  5: 'm',
  6: 'm',
  7: 'f',
  8: 'f',
  9: 'f'},
 'Age': {0: '[30-65]',
  1: '[30-65]',
  2: '[30-65]',
  3: '[30-65]',
  4: '[<30]',
  5: '[30-65]',
  6: '[<30]',
  7: '[30-65]',
  8: '[30-65]',
  9: '[<30]'}}

The problem here is that it keeps the index in the dictionary and in most cases this is not what we want. We will show you 2 more methods that are not using the index.

Records

df.to_dict('r')
[{'Gender': 'm', 'Age': '[30-65]'},
 {'Gender': 'm', 'Age': '[30-65]'},
 {'Gender': 'f', 'Age': '[30-65]'},
 {'Gender': 'm', 'Age': '[30-65]'},
 {'Gender': 'm', 'Age': '[<30]'},
 {'Gender': 'm', 'Age': '[30-65]'},
 {'Gender': 'm', 'Age': '[<30]'},
 {'Gender': 'f', 'Age': '[30-65]'},
 {'Gender': 'f', 'Age': '[30-65]'},
 {'Gender': 'f', 'Age': '[<30]'}]

List

df.to_dict('l')
{'Gender': ['m', 'm', 'f', 'm', 'm', 'm', 'm', 'f', 'f', 'f'],
 'Age': ['[30-65]',
  '[30-65]',
  '[30-65]',
  '[30-65]',
  '[<30]',
  '[30-65]',
  '[<30]',
  '[30-65]',
  '[30-65]',
  '[<30]']}

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s