Predictive Hacks

How to Convert a Pandas Series to Data Frame

Pandas has a method (.to_frame()) to convert a Series to Data Frame. We will create a sample Pandas Data Frame:

import pandas as pd
 
df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [3, 4, 5, 6]})
df

Let’s call column B which is a Series.

df['B']

We can easily convert it to Data Frame by running:

df['B'].to_frame()

Another approach, not the most efficient though, is to run:

pd.DataFrame(df['B'])

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