Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Pandas Introduction

Pandas Series

Pandas DataFrame

Pandas Read Files

Pandas Some functions and properties

Pandas Math Function

Pandas Selection

Pandas Change Type

Pandas Concatenate & Split

Pandas Sorting

Pandas Filter

Pandas Data Cleaning

Pandas Group by

Pandas Time Series

Pandas Analysis 1

Pandas Analysis 2

Pandas Analysis 3

Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Everything about pandas series

What is series?

A series is a one-dimensional labeled array or we can say it is a single list which has index number. Here we have two things, 1. index and 2. value.
It can hold data of any type (integer, string, float, python objects, etc). Pandas series is the same as a column in an excel sheet or we can compare it with a single column of a table. Using pandas we can create a series and can perform many works like fill the empty cell, sorting, mathematical calculations, filter data from the series, etc.

Example of a series:

Grade
0 A+
1 B+
2 A
3 B-
4 C
5 B-
6 A+

Here in the table, we can see a column with index numbers. It is a series. In the table, if we have multiple columns then an individual column is a series. By joining multiple series we can create a table or data frame.

How to create series?

To create a series in pandas, series() function is used. In the bracket of series function, pass a list, dictionary, or variable name(which contains a list or dictionary).

Example 1:

Input
import pandas as pd
x=[1,"data frame","python","data science"]
y=pd.Series(x)
print(y)
Output
0      1
1      data frame
2      python
3      data science
dtype: object

Here 0,1 is the index number

In the example 1 we created a series using a python list.

Example 2:

Input
import pandas as pd
y=pd.Series([1,"data frame","self","Taught",2,3])
print(y)
Output
0    1
1    True
2    Taught
3    2
4    3
dtype: object

Here 0,1,2,3,4 is the index number

Example 3:

Input
import pandas as pd
x={1:"a",2:"b",3:"c"}
y=pd.Series(x)
print(y)
Output
1     a
2     b
3     c
dtype: object

If you use a dictionary to create a series then all the keys of the dictionary will become the index number.

index parameter of series function

By using the index parameter you can change the series index numbers. To do this write index then use a equal sign(=) and then pass the new index numbers as a list.

Input
import pandas as pd
x=[1,"data frame","self",2,3]
y=pd.Series(x,index=["a","b","c","d","e"])
print(y)
Output
a     1
b     data frame
c     self
d     2
e     3
dtype: object

In the example we created our own custom index for the series.

dtype parameter of series function

By using dtype parameter you can change the series data type.

Input
import pandas as pd
x=[1,2,3]
y=pd.Series(x,dtype=float)
print(y)
Output
0    1.0
1    2.0
2    3.0
dtype: float64

In the example list we have integer elements or numbers but in series we have float numbers. It happens because of dtype parameter of series function.

name parameter of series function

By using the name parameter you can give a name of a series.

Input
import pandas as pd
x=[1,2,3]
y=pd.Series(x,name="Rafsun")
print(y)
Output
0    1.0
1    2.0
2    3.0
Name: Rafsun, dtype: float64

In the example we gave a name to the list Rafsun by using name parameter.

How perform slicing on a series?

Using the series index number we can get a particular value or a particular range of value from the series. In the series the index number starts from 0 and the end index number is n-1.

Example 1:

Example:1
Input
import pandas as pd
x=[1,2,3,4,5,6]
y=pd.Series(x)
z=y[2]
print(z)
Output
3

In the example we pass 2 in the third bracket so the get the third index value of the series. As we know the indexing starts from 0 so if count then we will see the third element index number is 2.

Example 2:

Input
import pandas as pd
x=[1,2,3,4,5,6]
y=pd.Series(x)
z=y[2:5]
print(z)
Output
2    3
3    4
4    5
dtype: int64

Example 3:

Input
import pandas as pd
x=[1,2,3,4,5,6]
y=pd.Series(x)
z=y[-3:]
print(z)
Output
3    4
4    5
5    6
dtype: int64

Example 4:

Input
import pandas as pd
x=[1,2,3,4,5,6]
y=pd.Series(x)
z=y[-5:-3]
print(z)
Output
1    2
2    3
dtype: int64

How to perform mathematical operation on series?

We can also perform mathematical operations like subtraction, addition, multiplication, division, etc on a series.

Example 1:

Input
import pandas as pd
x=[1,2,3]
z=[4,5,6]
y=pd.Series(x)
a=pd.Series(z)
v=y+a
print(v)
Output
0    5
1    7
2    9
dtype: int64

In the example 1 we have two lists which we converted into a series. Then we do addition of these two series. So you can also perform mathematical operations between two series but the list size must be same.

Example 2:

Input
import pandas as pd
x=[1,2,3]
z=[4,5,6]
y=pd.Series(x)
a=pd.Series(z)
v=y**a
print(v)
Output
0    1
1    32
2    729
dtype: int64

Example 3:

Input
import pandas as pd
z=[4,5,6]
a=pd.Series(z)
v=a/2
print(v)
Output
0    2.0
1    2.5
2    3.0
dtype: float64

Example 4:

Input
import pandas as pd
z=[4,5,6]
a=pd.Series(z)
v=a**2
print(v)
Output
0    16
1    25
2    36
dtype: float64

How to use condition to get series values?

You can get series data using condition. Here you will use a condition according to your need and according to the given condition you will get the output.

Example 1:

Input
import pandas as pd
x=[1,2,3,4,5,6]
y=pd.Series(x)
v=y[y%2==0]
print(v)
Output
1    2
3     4
5    6
dtype: int64

Example 2:

Input
import pandas as pd
x=[1,2,3,4,5,6]
y=pd.Series(x)
v=y[y>3]
print(v)
Output
3    4
4    5
5    6
dtype: int64

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.