Create pandas DataFrame from Series in Python (Example Code)

This article illustrates how to transform a pandas Series to a DataFrame column in the Python programming language.

Example Data

import pandas as pd                                # Import pandas
srs = pd.Series(['x', 'a', 'f', 'x', 'x', 'f'])    # Construct pandas Series
print(srs)
# 0    x
# 1    a
# 2    f
# 3    x
# 4    x
# 5    f
# dtype: object

Example: Convert pandas Series to DataFrame Column

df = srs.to_frame('my_col')                        # Apply to_frame() function
print(df)
#   my_col
# 0      x
# 1      a
# 2      f
# 3      x
# 4      x
# 5      f

Further Resources

Below, you may find some further resources on topics such as lists and data conversion.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top