Create Pandas Series using NumPy functions
A Pandas Series is like a one-dimensional array which can store numbers, text or other types of data. NumPy has built-in functions that generate sequences, random values or repeated numbers In this article, we’ll learn how to create a Pandas Series using different functions from the NumPy library.
Method 1: Using numpy.linspace()
The linspace() function creates a list of evenly spaced numbers from start
to stop
with num
total values. It’s used when you want to divide a range into equal parts.
import numpy as np
import pandas as pd
ser1 = pd.Series(np.linspace(3, 33, 3))
print(ser1)
ser2 = pd.Series(np.linspace(1, 100, 10))
print("\n", ser2)
Output:
Method 2: Using np.random.normal and np.random.rand()
These functions are used when you want to create random test data.
- random.normal() gives random numbers from a normal distribution. You can set the average, standard deviation and how many values to generate.
- random.rand gives random numbers between 0 and 1 with a uniform distribution.
import pandas as pd
import numpy as np
ser3 = pd.Series(np.random.normal())
print(ser3)
ser4 = pd.Series(np.random.normal(0.0, 1.0, 5))
print("\n", ser4)
ser5 = pd.Series(np.random.rand(10))
print("\n", ser5)
Output:

Using random.normal() and random.rand()
The first output is a single random number from a normal distribution. The second output shows five random float numbers also from a normal distribution. The third output has ten random numbers between 0 and 1 generated from a uniform distribution.
Method 3: Using numpy.repeat()
The numpy.repeat() function repeats a specific value multiple times. It’s used when you need to create a Series with the same value repeated.
import pandas as pd
import numpy as np
ser = pd.Series(np.repeat(0.08, 7))
print("\n", ser)
Output:

Using repeat method
In the above output we can see that 0.08 is repeating 8 times. With these methods we can easily create pandas series using Numpy.