Categories: PandasPython

How to create Pandas data structure Series 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 is a library module designed in Python programming. It is specific for data structure with labeled data. Pandas provides two data structure types, Series and Data Frame. Series is a one-dimensional labeled data, and Data Frame is used for tabular data storage.

Series stores data values and their corresponding labels in two arrays: its value part and index part. To create a Series, function Series() from Pandas can be just applied. Following example show how to create a Series.


#import module Numpy and Pandas 
import pandas as pd
import numpy as np
#create a Series, T, with input an array of 4 elements
T = pd.Series([32, 28, 19, 301])
T
#output
Out[139]: 
0     32
1     28
2     19
3    301
dtype: int64

We can see that Pandas assign index from 0 by default to the Series. Next example shows creating a Series with manually assigning its index.

#create a Series, manually assigning its index
P = pd.Series([32, 28, 19, 301], index=['l', 'm', 'n', 'o'])
P
#output
Out[140]: 
l     32
m     28
n     19
o    301
dtype: int64
#Series's values part
P.values
#output
array([ 32,  28,  19, 301], dtype=int64)
#Series's index part
P.index
#output
Index(['l', 'm', 'n', 'o'], dtype='object')

A Series’s internal elements can be returned with square brackets and the corresponding index.

#value of Series, associated with third index
P[2]
#output
19
#element of Series, associated with index 'o'
P['o']
#output
301
#elements of Series, associated with first to third indices 
P[0:3]
#output, is another Series
l    32
m    28
n    19
dtype: int64
#elements of Series, associated with indices 'm' and 'o' 
P[['m','o']]
#output, is another Series
m     28
o    301
dtype: int64

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