Built with Tutify.io

    AIStudyLabs

    Create Pandas Series in Python

    Create Pandas Series in Python

    You can create pandas Series in multiple ways for example creating from the python list, converting dictionary to Series, create series from numpy array, and initializing from the series constructor. In this article, I will cover all these different ways to initialize the Series with examples.

    Key Points –

    <a id="h-what-is-the-pandas-series"></a>

    What is the Pandas Series?

    In Python's Pandas library, a Series is a one-dimensional array-like object that can hold any data type such as integers, floats, strings, or even Python objects. It's similar to a list or an array, but with additional functionalities and capabilities. Each element in a Series has a label called an index. This index helps in accessing and manipulating data efficiently.

    <a id="h-quick-examples-of-create-pandas-series"></a>

    Quick Examples of Create Pandas Series

    If you hurry below are quick examples of creating the Pandas Series.

    
    # Quick examples of create pandas series
    
    # Example 1: Create empty Series
    ser = pd.Series()
    
    # Example 2: Create Series from array
    data = np.array(['python', 'php', 'java'])
    series = pd.Series(data)
    
    # Example 3: Create Pandas Series with custom index
    series = pd.Series(data=data, index=['r1', 'r2', 'r3'])
    
    # Example 4: Create Pandas Series using list
    data = ['python', 'php', 'java']
    series = pd.Series(data, index=['r1', 'r2','r3'])
    
    # Example 5: Create Series from Dict
    data = {'Courses' :"pandas", 'Fees' : 20000, 'Duration' : "30days"}
    series = pd.Series(data)
    
    # Example 6: Convert Single column DataFrame to Series
    data =  ["Python", "PHP", "Java"]
    df = pd.DataFrame(data, columns = ['Courses'])
    series = df.squeeze()
    
    # Example 7: Create Series from Pandas DataFrame column
    series = df['Fee'].squeeze()
    
    # Example 8: Create Series from Pandas DataFrame row
    series = df.iloc[2].squeeze()
    

    <a id="h-pandas-series-constructor"></a>

    Pandas.series() Constructor

    Below is the syntax of pandas Series Constructor.

    
    # Pandas Series Constructor Syntax
    Pandas.series(data, index, dtype, copy)
    

    <a id="h-create-empty-pandas-series-from-constructor"></a>

    Create Empty Pandas Series from Constructor

    Pandas provide Series() constructor to create or initialize a Series. Default constructor without any arguments creates an empty Series object. This function takes data as an argument that accepts ndarray, list, dict, and constants.

    
    # Imports
    import pandas as pd
    
    # Create empty pandas series
    ser = pd.Series()
    print(ser)
    
    # Output :
    # Series([], dtype: float64)
    

    <a id="h-create-a-series-from-numpy-array"></a>

    Create a Series From NumPy Array

    NumPy array is a data structure (usually numbers) that contains values of the same type, similar to a list. But arrays are more efficient than Python lists and also much more compact. Sometimes you would be required to create pandas Series from the NumPy array. Let's create Numpy array using numpy.array() function then, create a Series by passing an array into series() function.

    
    # Imports
    import pandas as pd
    import numpy as np
    
    # Create Series from array
    data = np.array(['python', 'php', 'java'])
    series = pd.Series(data)
    print (series)
    

    Yields below output.

    
    # Output:
    0    python
    1       php
    2      java
    dtype: object
    

    By default, the index of the Series starts from 0, if we want to customize the index of the Series, we can change it by providing a list of values to the index parameter to the Series() function.

    <a id="h-customize-the-index-of-series"></a>

    Customize the index of Series.

    Now, let’s see how to create a pandas Series with a custom Index. To do so, will use index param which takes a list of index values. Make sure the index list matches the data size. For example,

    
    # Create pandas DataFrame with custom index
    series = pd.Series(data=data, index=['r1', 'r2', 'r3'])
    print(series)
    

    Yields below output.

    
    # Output:
    r1    python
    r2       php
    r3      java
    dtype: object
    

    <a id="h-create-series-from-list"></a>

    Create Series from List

    If you have a Python list it can be easily converted into Pandas Series by passing the list object as an argument to series() function. In case if you wanted to convert the Pandas Series to a list use Series.tolist().

    
    # Create Pandas Series from list
    data = ['python', 'php', 'java']
    series = pd.Series(data, index=['r1', 'r2','r3'])
    print(series)
    

    Yields below output.

    
    # Output:
    r1 python
    r2 php
    r3 java
    dtype:object
    

    <a id="h-create-a-series-using-a-dictionary"></a>

    Create a Series using a Dictionary

    A Python dictionary can be utilized to create a Pandas Series. In this process, the dictionary keys are used as the index labels, and the dictionary values are used as the data for the Series. If you want to convert a Pandas Series to a dictionary, use the Series.to_dict() method.

    
    # Create a Dict from a input
    data = {'Courses' :"pandas", 'Fees' : 20000, 'Duration' : "30days"}
    series = pd.Series(data)
    print (series)
    

    Yields below output.

    
    # Output:
    Courses     pandas
    Fees         20000
    Duration    30days
    dtype: object
    

    <a id="h-create-pandas-series-from-dataframe"></a>

    Create pandas Series from DataFrame

    In this section let's see how to convert DataFrame to Series. Note that every column in a DataFrame is a Series. hence, we can convert single or multiple columns into Series.

    1. Single DataFrame column into a Series (from a single-column DataFrame)
    2. Specific DataFrame column into a Series (from a multi-column DataFrame)
    3. Single row in the DataFrame into a Series

    <a id="h-create-a-series-from-single-dataframe-column"></a>

    Create a Series from single DataFrame column

    Let’s create a DataFrame with a single column. By using DataFrame.squeeze() to create Series from the DataFrame. For example,

    
    # Create DataFrame with single column
    data =  ["Python", "PHP", "Java"]
    df = pd.DataFrame(data, columns = ['Courses'])
    series = df.squeeze()
    print(series)
    print (type(series))
    

    Yields below output.

    
    # Output:
    0    Python
    1       PHP
    2      Java
    Name: Courses, dtype: object
    

    <a id="h-create-a-series-from-a-specific-dataframe-column"></a>

    Create a Series From a specific DataFrame column

    If you have a DataFrame with multiple columns, use the below example to convert a specific column into a series. Let's create Pandas DataFrame with multiple columns.

    
    # create DataFrame with multiple columns
    import pandas as pd
    data = {'Courses': ['Spark', 'PySpark', 'Python'],
            'Duration':['30 days', '40 days', '50 days'],
            'Fee':[20000, 25000, 26000]
            }
    df = pd.DataFrame(data, columns = ['Courses', 'Duration', 'Fee'])
    print(df)
    print (type(df))
    

    Let's convert the Fee column into a Series.

    
    # Create Series using Pandas DataFrame
    series = df['Fee'].squeeze()
    

    Yields below output.

    
    # Output:
    0    20000
    1    25000
    2    26000
    Name: Fee, dtype: int64
    

    <a id="h-create-a-series-from-dataframe-row"></a>

    Create a Series from DataFrame Row

    Above, we have seen converting DataFrame column into Series, here, I will explain converting rows into Series. For example,

    
    # Create Series using Pandas DataFrame row
    series = df.iloc[2].squeeze()
    print(series)
    print (type(series))
    

    Yields below output.

    
    # Output:
    Courses      Python
    Duration    50 days
    Fee             NaN
    Name: 2, dtype: object
    <class 'pandas.core.series.Series'>
    

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned the Pandas Series() method and using this function how we can create Pandas Series. You can create Series from the NumPy array, list, dictionary, DataFrame column/rows e.t.c

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References