Pandas Series To DataFrame – pd.Series.to_frame()

Pandas offers two main datatypes, Series and DataFrames. They are the building blocks of data analysis within python. You may want to convert a series to a DataFrame and that is where .to_frame() comes in.

pandas.Series.to_frame()

Series = Pandas Series is a one-dimensional labeled (it has a name) array which holds data. The axis (think of these as row names) are called index. Simply, a Pandas Series is like an excel column.

DataFrame = A collection of series. Each series name will be the column name.

Pseudo Code: Convert your Pandas Series into a single column Pandas DF

Pandas Series To DataFrame

.to_frame() Parameters

name (Default: None) = By default, the new DF will create a single column with your Series name as the column name. However, if you wanted to change that, you can specify a new name here.

Now the fun part, let’s take a look at a code sample

In [17]:
import pandas as pd

Pandas Series To Frame

Most people are comfortable working in DataFrame style objects. However, Pandas will also throw you a Series (quite often). In order to change your series into a DataFrame you call ".to_frame()"

Examples we'll run through:

  1. Changing your Series into a DataFrame
  2. Changing your Series into a DataFrame with a new name

Let's create two Series, one with a name, and one without

In [18]:
my_series_without_name = pd.Series([1, 2, 'e', pd.NA, 43, 'Bob'])
my_series_without_name
Out[18]:
0       1
1       2
2       e
3    <NA>
4      43
5     Bob
dtype: object
In [19]:
my_series_with_name = pd.Series(['a', 45, pd.NA, 43, 'Sally', 87], name='my_series')
my_series_with_name
Out[19]:
0        a
1       45
2     <NA>
3       43
4    Sally
5       87
Name: my_series, dtype: object

1. Changing your Series into a DataFrame

Let's change both of our series into DataFrames.

Notice how the one without a name has '0' as it's column name. The Series with a name has the series name as the column name

In [21]:
my_series_without_name.to_frame()
Out[21]:
0
01
12
2e
3<NA>
443
5Bob
In [22]:
my_series_with_name.to_frame()
Out[22]:
my_series
0a
145
2<NA>
343
4Sally
587

2. Changing your Series into a DataFrame with a new name

You may want to change the name of your new DataFrame column in general. By default it will be the Series name, but let's change it.

Here I'm going to call my new column 'my_new_df_column'

In [25]:
my_series_without_name.to_frame(name='my_new_df_column_no_name')
Out[25]:
my_new_df_column_no_name
01
12
2e
3<NA>
443
5Bob
In [26]:
my_series_with_name.to_frame(name='my_new_df_column_with_name')
Out[26]:
my_new_df_column_with_name
0a
145
2<NA>
343
4Sally
587

Link to code above

Check out more Pandas functions on our Pandas Page

Official Documentation