Converting a pandas DataFrame into a TensorFlow Dataset

Eric Ness
When I Work Data
Published in
1 min readAug 7, 2018
Photo by Raka Rachgo on Unsplash

Often when working on a deep learning model you will retrieve your source data in a pandas.DataFrame and need to convert it into a format that Tensorflow can read. Fortunately, Tensorflow now has Datasets which create data pipelines for your network to train on. Converting a DataFrame into a tf.data.Dataset is straight-forward. The code below shows how to take a DataFrame with 3 randomly generated features and 3 target classes and convert it into a TensorFlow Dataset. The code uses eager execution mode, but the code will also work when using an execution graph. After the DataSet is created, you can run batch , shuffle and other functions to pre-process the data before training.

Code

Output

--

--

Eric Ness
When I Work Data

Principal Machine Learning Engineer at C.H.Robinson, a Fortune 250 supply chain company.