- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a series from a NumPy array?
A pandas Series is very similar to a 1-dimensional NumPy array, and we can create a pandas Series by using a NumPy array. To do this we need to import the NumPy module, as it is a prerequisite for the pandas package no need to install it separately.
It is automatically installed by our package installer. So we can directly import the NumPy module into our workspace.
If you don’t like to use this available version of NumPy, which is installed by the package installer we can install our required version of the NumPy package, as it is also an open-source package like pandas.
import pandas as pd import numpy as np array = np.array([8,54,43,6,73,78]) s = pd.Series(array) print(s)
In the above example, we have created a pandas Series by using the NumPy array for that purpose we need to import the NumPy module also, initially we imported pandas and NumPy modules with the alias names pd and np respectively.
Then the variable array is created by using the NumPy.array method, this array is having integer values with a length of 6 elements. This array variable is the input data for pandas.Series method.
0 8 1 54 2 43 3 6 4 73 5 78 dtype: int32
The resultant pandas Series is shown in the above output block, here the labels are integer values from 0-5 which are auto-created by the pandas Series method, and the data is also integer data type (int 32).
import pandas as pd import numpy as np # create numpy array using array method array = np.array([1.2,5.5,2.9,4.6]) s = pd.Series(array, index=list('abcd')) print(s)
The above block is another example of pandas series created by using NumPy array, where the array is created by all floating-point numbers, and by using this array we will create a pandas Series and the index values are manually created by using string.
Here we have given a list of strings to the index attribute of our pandas Series method.
a 1.2 b 5.5 c 2.9 d 4.6 dtype: float64
This series has 4 elements and each element is a float64 data type. The index values are represented by labels which are a,b,c,d.
- Related Articles
- Create a Series from a List, Numpy Array, and Dictionary in Pandas
- Convert a NumPy array to a Pandas series
- Create a record array from a (flat) list of array in Numpy
- How to create a numpy array within a given range?
- Create a boolean mask from an array in Numpy
- Create a record array from binary data in Numpy
- How to create a Pandas series from a python dictionary?
- How to create a series from a list using Pandas?
- How to create an empty and a full NumPy array?
- Create a new array from the masked array and return a new reference in Numpy
- How to create a string from a Java Array?
- Create a record array from a (flat) list of array and fetch arrays using names in Numpy
- How to sort a Numpy Array?
- Create a record array from a (flat) list of array and set a valid datatype for all in Numpy
- Python Pandas - Create a Series from TimeDeltaIndex