Pandas Tutorial Pandas References

Pandas - Series



Pandas Series is a one-dimensional labeled array. It can hold data of any type like integer, string, float, python objects, etc. The axis labels are collectively called index. It is like a column in an excel sheet.

Create Series

A Pandas Series can be created using Series() constructor. The syntax for using the function is given below:

Syntax

pandas.Series(data, index, dtype, name, copy)

Parameters

data Optional. Specify data. It takes data in various forms like list, ndarray, dict and constants.
index Optional. Specify value of the index. It must be unique, hashable and same length as data. The Default np.arrange(n) index is used if no index is passed.
dtype Optional. Specify data type of the Series.
name Optional. Specify name to give to the Series.
copy Optional. Specify True to copy data from inputs, False otherwise. Default is False.

Create an empty Series

An empty Series can be created by passing no arguments in the Series() constructor as shown below:

Example:

import pandas as pd
x = pd.Series()

print(x)

The output of the above code will be:

Series([], dtype: float64)

Create a Series from List

In the example below, a list called Name is used to create a series. As the index is not provided, therefore by default, np.arrange(n) is used for axis labels.

Example:

import pandas as pd
Name = ['John', 'Marry', 'Jo', 'Sam']
x = pd.Series(Name)

print(x)

The output of the above code will be:

0     John
1    Marry
2       Jo
3      Sam
dtype: object

Example:

When the index is provided to series, the axis labels gets changed as shown below:

import pandas as pd
Name = ['John', 'Marry', 'Jo', 'Sam']
x = pd.Series(Name, index=['P1', 'P2', 'P3', 'P4'])

print(x)

The output of the above code will be:

P1     John
P2    Marry
P3       Jo
P4      Sam
dtype: object

Create a Series from ndarray

A ndarray can also be passed to get a series. Here, the index is not provided, therefore by default, np.arrange(n) is used for axis labels.

Example:

import pandas as pd
import numpy as np
Name = np.array(['John', 'Marry', 'Jo', 'Sam'])
x = pd.Series(Name)

print(x)

The output of the above code will be:

0     John
1    Marry
2       Jo
3      Sam
dtype: object

Example:

When the index is provided to series, the axis labels gets changed as shown below:

import pandas as pd
import numpy as np
Name = np.array(['John', 'Marry', 'Jo', 'Sam'])
x = pd.Series(Name, index=['P1', 'P2', 'P3', 'P4'])

print(x)

The output of the above code will be:

P1     John
P2    Marry
P3       Jo
P4      Sam
dtype: object

Create a Series from dict

A dictionary can also be used to create a series. If index is not provided then the keys of the dictionary (in sorted order) will be used to construct index of the series. If index is passed, NaN is appended for the missing data.

Example:

import pandas as pd
Name = {"P1":"John", "P2":"Marry", "P3":"Sam"}
x = pd.Series(Name)

print(x)

The output of the above code will be:

P1     John
P2    Marry
P3      Sam
dtype: object

Example:

In the example below, index is passed with no data for 'P4'. Therefore, NaN is appended for the missing data.

import pandas as pd
Name = {"P1":"John", "P2":"Marry", "P3":"Sam"}
x = pd.Series(Name, index=['P1', 'P2', 'P3', 'P4'])

print(x)

The output of the above code will be:

P1     John
P2    Marry
P3      Sam
P4      NaN
dtype: object

Create a Series from Scalar

When a scalar is used to create a series, the value is repeated for all index.

Example:

import pandas as pd
x = pd.Series(10, index=['P1', 'P2', 'P3', 'P4'])

print(x)

The output of the above code will be:

P1    10
P2    10
P3    10
P4    10
dtype: int64

Accessing Data from Series with Position

The data from a series can be accessed in the similar manner as of ndarray.

Example:

In the example below, first element of the series is accessed which is stored at the zeroth position in the series.

import pandas as pd
Name = ['John', 'Marry', 'Ramesh', 'Jo', 'Sam']
x = pd.Series(Name)

#print first element of the series
print(x[0])

The output of the above code will be:

John

Example:

A range of elements can be accessed using : operator.

import pandas as pd
Name = ['John', 'Marry', 'Ramesh', 'Jo', 'Sam']
x = pd.Series(Name)

#printing first two elements of the series
print(x[:2])

print()
#printing last two elements of the series
print(x[-2:])

The output of the above code will be:

0     John
1    Marry
dtype: object

3     Jo
4    Sam
dtype: object

Accessing Data from Series with Label (index)

Example:

In the example below, element of the series is accessed using its label.

import pandas as pd
Name = ['John', 'Marry', 'Ramesh', 'Jo', 'Sam']
x = pd.Series(Name, index=['P1', 'P2', 'P3', 'P4', 'P5'])

#print element of the series with label 'P3'
print(x['P3'])

The output of the above code will be:

Ramesh

Example:

Multiple elements of the series also be accessed using its label as shown below:

import pandas as pd
Name = ['John', 'Marry', 'Ramesh', 'Jo', 'Sam']
x = pd.Series(Name, index=['P1', 'P2', 'P3', 'P4', 'P5'])

#print element of the series with label 'P3'
print(x[['P1', 'P3', 'P5']])

The output of the above code will be:

P1      John
P3    Ramesh
P5       Sam
dtype: object