• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:12 mins read
You are currently viewing Create Pandas Series in Python

We 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.

Advertisements

1. What is Pandas Series?

Pandas Series is a one-dimensional array that is capable of storing various data types (integer, string, float, python objects, etc.). We can easily convert the list, tuple, and dictionary into Series using the pandas.Series() function. The row labels of the Series are called the index. The Series can have only one column. A List, NumPy Array, and Dict can be turned into a pandas Series.

2. Quick Examples of Create Pandas Series

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


# Below are the quick examples
# 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()
print(series)

3. Pandas.series() Constructor

Below is the syntax of pandas Series Constructor.


# Pandas Series Constructor Syntax
Pandas.series(data, index, dtype, copy)
  • data: The data contains ndarray, list, and constants.
  • Index: The index must be unique and hashable. np.arange(n) if no index is passed.
  • dtype: By default, the data type of Series is float. 
  • copy: It is used to copy the data. The data contains ndarray, list, and constants.

4. 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)

5. 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.

5.1 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

6. 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

7. Create Series using a Dictionary

A Python Dictionary can be used to create a pandas Series. Keys from Dict are used as Index and values are used as a column in Pandas Series. In case you wanted to convert Pandas Series to the dictionary use the Series.to_dict() function.


# 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

8. 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

8.1 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

8.2. 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

8.3 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'>

9. Conclusion

In this article, I have explained the Pandas Series() function 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

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium