pandasPython

Pandas Series

A Series is a one-dimensional labeled data structure that can contain data of any type (integer, float, etc.). The labels are known as index. A Series contain homogeneous data.

A Series can be visualized as a one-dimensional NumPy array, for example:

RedBlueGreenBlackYellowWhiteBrown

The size of a Series is immutable but data values are mutable.

Creating a Series

The syntax for creating a Pandas Series is:

pandas.DataFrame( data, index, dtype, copy)

data, index, columns, dtype, copy are its parameters –

  • data can take constant values or lists, ndarray, etc.
  • index contains the unique indexing values
  • dtype is the for the data type of the series
  • copy is for copying of data

Pandas Series can be created from constant values, lists, maps, dictionary, ndarray, etc. Let us look at some basic examples of creating a Series.

Creating an empty Series

import pandas as pd

# creating an empty Series
series = pd.Series()
print(series)

The output obtained is:

Series([], dtype: float64)

Creating a Series using list

import pandas as pd

# creating a Series using list
student = [‘John’,15,87.5]
series = pd.Series(student)
print(series)

This gives the following output:

0 John
1 15
2 87.5
dtype: object

Creating a Series using list with labels

import pandas as pd

# creating a Series using list with labels
student = [‘John’,15,87.5]
labels = [‘Name’,’Age’,’Marks’]
series = pd.Series(student,labels)
print(series)

Output is:

Name John
Age 15
Marks 87.5
dtype: object

Creating a Series using Dictionary

import pandas as pd

# a dictionary
student = {‘Name’: ‘John’, ‘Age’: 20, ‘Marks’: 87.5}
# creating a Series using dictionary
series = pd.Series(student,index=[‘Name’,’Age’,’Country’,’Marks’])
print(series)

The output obtained is as follows:

Name John
Age 20
Country NaN
Marks 87.5
dtype: object

The missing values are filled with NaN.

Summary

In this article, we have looked at the main data structures of Pandas – Series. In the upcoming articles, we will focus on more operations on DataFrame and Series.

Leave a Reply

Your email address will not be published. Required fields are marked *