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 R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago