Predictive Hacks

How to Convert a Pandas DataFrame to TensorFlow Dataset

Many TensorFlow examples are using TensorFlow Datasets. So the question is, how the heck you can convert your Pandas Data Frame to TensorFlow Datasets? In the example below, we create a dummy Pandas Data Frame and we convert it to a TensorFlow Dataset.

import tensorflow as tf
import pandas as pd

df = pd.DataFrame({'Col1':[1,2,3,4,5],
                   'Col2': ['a','b','c','d','e']})

df

Let’s convert it to a TensorFlow Dataset.

tf_df = tf.data.Dataset.from_tensor_slices(dict(df))
tf_df
 

Let’s print the first 5 rows.

for k in tf_df.take(5):
    print(k)

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