Categories: PandasPython

How to create Pandas Series from Numpy array in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

Pandas module in Python provides two data structures, namely Series and Data Frame. While a Data Frame is a tabular dataset has similar mechanism like a spreadsheet, a Series is just a one-dimensional data object with labels. Creating a Series can come from manually input values, with pd.Series() functio, or by inputting a Numpy array with the same function, because Pandas module builds upon Numpy. Next example shows creating a Series from simple array of 4 elements.

#Import Pandas and Numpy module
import pandas as pd
import numpy as np
#create an array from Numpy
TA = np.array([32, 19, 301, 7])
TA
#output
array([ 32,  19, 301,   7])
#create a Series, by inputting Numpy array
PS = pd.Series(TA)
PS
#output
0     32
1     19
2    301
3      7
dtype: int32

We have seen the process of creating Series from inputting array is quite simple. However, we have to note that both the array and Series point to the same object in Python working session. If we change the value of one of them, the other will change automatically too. Next example shows this mechanism.

#change the value of element of array
TA[3] = 13
TA
#output
array([ 32,  19, 301,  13])
PS
#output, corresponding element in Series changed too
0     32
1     19
2    301
3     13
dtype: int32
#change the value of element in Series
PS[3] = 32
PS
#output
0     32
1     19
2    301
3     32
TA
#output, corresponding value of element in array changed too
array([ 32,  19, 301,  32])

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

4 days ago

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

4 weeks ago

Install PyTorch on Windows

PyTorch is a deep learning package for machine learning, or deep learning in particular for…

1 month ago

Topic Modeling using Latent Dirichlet Allocation with Python

Topic modeling is a subcategory of unsupervised machine learning method, and a clustering task in…

2 months ago

Document sentiment classification using bag-of-words in Python

For online Python training registration, click here ! Sentiment classification is a type of machine…

2 months ago

Download R Course source files

Click here to download R Course source files !

11 months ago

Warning: Cannot modify header information - headers already sent by (output started at /home/idphekra/public_html/rdatacode.com/wp-content/plugins/w3-total-cache/Util_File.php:158) in /home/idphekra/public_html/rdatacode.com/wp-content/plugins/accelerated-mobile-pages/includes/vendor/amp/amp.php on line 100