Create pandas Series from DataFrame in Python (Example Code)

In this tutorial you’ll learn how to transform a pandas DataFrame to a Series in Python.

Creation of Example Data

my_df = pd.DataFrame([list(range(7))],    # Construct pandas DataFrame
                     columns=["col{}".format(i) for i in range(7)])
print(my_df)
#    col0  col1  col2  col3  col4  col5  col6
# 0     0     1     2     3     4     5     6

Example: Convert pandas DataFrame with One Row to Series

my_srs = my_df.squeeze(axis = 0)          # Create pandas Series
print(my_srs)
# col0    0
# col1    1
# col2    2
# col3    3
# col4    4
# col5    5
# col6    6
# Name: 0, dtype: int64

Related Tutorials

Have a look at the following tutorials. They illustrate 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