TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Pandas Series

Python Pandas Series with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PYTHON

Python Pandas Series

The Pandas Series can be defined as a one-dimensional array that is capable of storing various data types. We can easily convert the list, tuple, and dictionary into series using "series' method. The row labels of series are called the index. A Series cannot contain multiple columns. It has the following parameter:

  • data: It can be any list, dictionary, or scalar value.
  • index: The value of the index should be unique and hashable. It must be of the same length as data. If we do not pass any index, default np.arrange(n) will be used.
  • dtype: It refers to the data type of series.
  • copy: It is used for copying the data.

Creating a Series:

We can create a Series in two ways:

  1. Create an empty Series
  2. Create a Series using inputs.

Create an Empty Series:

We can easily create an empty series in Pandas which means it will not have any value.

The syntax that is used for creating an Empty Series:

<series object> = pandas.Series()

The below example creates an Empty Series type object that has no values and having default datatype, i.e., float64.

Example

import pandas as pd
x = pd.Series()
print (x)

Output

Series([], dtype: float64)

Creating a Series using inputs:

We can create Series by using various inputs:

  • Array
  • Dict
  • Scalar value

Creating Series from Array:

Before creating a Series, firstly, we have to import the numpy module and then use array() function in the program. If the data is ndarray, then the passed index must be of the same length.

If we do not pass an index, then by default index of range(n) is being passed where n defines the length of an array, i.e., [0,1,2,....range(len(array))-1].

Example

import pandas as pd
import numpy as np
info = np.array(['P','a','n','d','a','s'])
a = pd.Series(info)
print(a) 

Output

0    P
1    a
2    n
3    d
4    a
5    s
dtype: object

Create a Series from dict

We can also create a Series from dict. If the dictionary object is being passed as an input and the index is not specified, then the dictionary keys are taken in a sorted order to construct the index.

If index is passed, then values correspond to a particular label in the index will be extracted from the dictionary.

#import the pandas library 
import pandas as pd
import numpy as np
info = {'x' : 0., 'y' : 1., 'z' : 2.}
a = pd.Series(info)
print (a)

Output

x     0.0
y     1.0
z     2.0
dtype: float64

Create a Series using Scalar:

If we take the scalar values, then the index must be provided. The scalar value will be repeated for matching the length of the index.

#import pandas library 
import pandas as pd
import numpy as np
x = pd.Series(4, index=[0, 1, 2, 3])
print (x)

Output

0      4
1      4
2      4
3      4
dtype: int64

Accessing data from series with Position:

Once you create the Series type object, you can access its indexes, data, and even individual elements.

The data in the Series can be accessed similar to that in the ndarray.

import pandas as pd
x = pd.Series([1,2,3],index = ['a','b','c'])
#retrieve the first element
print (x[0])

Output

1 

Series object attributes

The Series attribute is defined as any information related to the Series object such as size, datatype. etc. Below are some of the attributes that you can use to get the information about the Series object:

Attributes Description
Series.index Defines the index of the Series.
Series.shape It returns a tuple of shape of the data.
Series.dtype It returns the data type of the data.
Series.size It returns the size of the data.
Series.empty It returns True if Series object is empty, otherwise returns false.
Series.hasnans It returns True if there are any NaN values, otherwise returns false.
Series.nbytes It returns the number of bytes in the data.
Series.ndim It returns the number of dimensions in the data.
Series.itemsize It returns the size of the datatype of item.

Retrieving Index array and data array of a series object

We can retrieve the index array and data array of an existing Series object by using the attributes index and values.

import numpy as np 
import pandas as pd 
x=pd.Series(data=[2,4,6,8]) 
y=pd.Series(data=[11.2,18.6,22.5], index=['a','b','c']) 
print(x.index) 
print(x.values) 
print(y.index) 
print(y.values)

Output

RangeIndex(start=0, stop=4, step=1)
[2 4 6 8]
Index(['a', 'b', 'c'], dtype='object')
[11.2 18.6 22.5]

Retrieving Types (dtype) and Size of Type (itemsize)

You can use attribute dtype with Series object as <objectname> dtype for retrieving the data type of an individual element of a series object, you can use the itemsize attribute to show the number of bytes allocated to each data item.

import numpy as np 
import pandas as pd 
a=pd.Series(data=[1,2,3,4]) 
b=pd.Series(data=[4.9,8.2,5.6], 
index=['x','y','z']) 
print(a.dtype) 
print(a.itemsize)  
print(b.dtype) 
print(b.itemsize)

Output

int64
8
float64
8

Retrieving Shape

The shape of the Series object defines total number of elements including missing or empty values(NaN).

import numpy as np 
import pandas as pd 
a=pd.Series(data=[1,2,3,4]) 
b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z']) 
print(a.shape) 
print(b.shape)

Output

(4,)
(3,)

Retrieving Dimension, Size and Number of bytes:

import numpy as np 
import pandas as pd 
a=pd.Series(data=[1,2,3,4]) 
b=pd.Series(data=[4.9,8.2,5.6],
index=['x','y','z']) 
print(a.ndim, b.ndim) 
print(a.size, b.size) 
print(a.nbytes, b.nbytes)

Output

1 1
4 3
32 24

Checking Emptiness and Presence of NaNs

To check the Series object is empty, you can use the empty attribute. Similarly, to check if a series object contains some NaN values or not, you can use the hasans attribute.

Example

import numpy as np 
import pandas as pd 
a=pd.Series(data=[1,2,3,np.NaN]) 
b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z']) 
c=pd.Series() 
print(a.empty,b.empty,c.empty) 
print(a.hasnans,b.hasnans,c.hasnans) 
print(len(a),len(b)) 
print(a.count( ),b.count( ))

Output

False   False   True
True    False   False
4   3
3   3

Series Functions

There are some functions used in Series which are as follows:

Functions Description
Pandas Series.map() Map the values from two series that have a common column.
Pandas Series.std() Calculate the standard deviation of the given set of numbers, DataFrame, column, and rows.
Pandas Series.to_frame() Convert the series object to the dataframe.
Pandas Series.value_counts() Returns a Series that contain counts of unique values.





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf