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.
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
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
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:
- Changing your Series into a DataFrame
- Changing your Series into a DataFrame with a new name
Let's create two Series, one with a name, and one without
my_series_without_name = pd.Series([1, 2, 'e', pd.NA, 43, 'Bob']) my_series_without_name
0 1 1 2 2 e 3 <NA> 4 43 5 Bob dtype: object
my_series_with_name = pd.Series(['a', 45, pd.NA, 43, 'Sally', 87], name='my_series') my_series_with_name
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
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'
Check out more Pandas functions on our Pandas Page