Categories: 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.

Pallavi Pandey

Share
Published by
Pallavi Pandey

Recent Posts

MapReduce Algorithm

In this tutorial, we will focus on MapReduce Algorithm, its working, example, Word Count Problem,…

1 month ago

Linear Programming using Pyomo

Learn how to use Pyomo Packare to solve linear programming problems. In recent years, with…

8 months ago

Networking and Professional Development for Machine Learning Careers in the USA

In today's rapidly evolving technological landscape, machine learning has emerged as a transformative discipline, revolutionizing…

10 months ago

Predicting Employee Churn in Python

Analyze employee churn, Why employees are leaving the company, and How to predict, who will…

1 year ago

Airflow Operators

Airflow operators are core components of any workflow defined in airflow. The operator represents a…

1 year ago

MLOps Tutorial

Machine Learning Operations (MLOps) is a multi-disciplinary field that combines machine learning and software development…

1 year ago