Pandas Series: Diverse Methods for Creating Series in Python

Python Pandas @ Freshers.in

Understanding Pandas Series

Definition

A Pandas Series is a one-dimensional array-like object capable of holding any data type. It is essentially a column in an Excel sheet, equipped with both a data column and an associated index. Pandas Series offers flexibility and efficiency in handling one-dimensional data.

Methods of Creating Series in Pandas

1. Creating a Series from a List

You can create a Series by passing a list of values. The index will be automatically assigned unless specified.

Example:

import pandas as pd
names = ['Sachin', 'Manju', 'Ram', 'Raju', 'David', 'Wilson']
series_from_list = pd.Series(names)

2. Series from a Dictionary

When a dictionary is used, the keys become the Series index.

Example:

data = {'Sachin': 45, 'Manju': 30, 'Ram': 25, 'Raju': 40, 'David': 55, 'Wilson': 60}
series_from_dict = pd.Series(data)

3. Series from a NumPy Array

Series can also be created from NumPy arrays, a useful approach when dealing with numerical data.

Example:

import numpy as np
ages = np.array([45, 30, 25, 40, 55, 60])
series_from_array = pd.Series(ages, index=['Sachin', 'Manju', 'Ram', 'Raju', 'David', 'Wilson'])

4. Series with Scalar Value

You can create a Series with a scalar value, which will be repeated to match the length of the index.

Example:

constant_series = pd.Series(10, index=['Sachin', 'Manju', 'Ram', 'Raju', 'David', 'Wilson'])

5. Series from a File

Series can be created by loading data from a file, such as a CSV.

Example:

For this example, imagine a CSV file names.csv with a single column of names.

series_from_csv = pd.read_csv('names.csv', squeeze=True)
Author: user