Pandas

1. Pandas Series: Creating a Series in Python

Pandas Series: Creating a Series in Python

What is a Pandas Series?

A Series in Pandas is a one-dimensional labeled array capable of holding data of any type (integer, float, string, etc.). It is similar to a column in an Excel spreadsheet or a list in Python but comes with added functionalities like indexing and built-in operations.

You can create a Series from:
✅ A Python list
✅ A NumPy array
✅ A dictionary


1. Creating a Pandas Series from a List

You can create a Pandas Series using a simple list.

Example 1: Creating a Series from a List

import pandas as pd  

# Creating a Series from a list
data = [10, 20, 30, 40, 50]  
series = pd.Series(data)  

print(series)

Output:

0    10  
1    20  
2    30  
3    40  
4    50  
dtype: int64  

🔹 Here, Pandas automatically assigns index values (0, 1, 2, …).


2. Creating a Pandas Series with Custom Index

By default, Pandas assigns numeric indexes (0, 1, 2, …), but you can define your own index.

Example 2: Creating a Series with Custom Index

import pandas as pd  

# Creating a Series with custom index  
data = [100, 200, 300, 400]  
index_labels = ['A', 'B', 'C', 'D']  
series = pd.Series(data, index=index_labels)  

print(series)

Output:

A    100  
B    200  
C    300  
D    400  
dtype: int64  

🔹 Here, we assigned custom labels (A, B, C, D) as the index.


3. Creating a Pandas Series from a Dictionary

A dictionary can also be used to create a Series, where the keys become the index and the values become the data.

Example 3: Creating a Series from a Dictionary

import pandas as pd  

# Creating a Series from a dictionary  
data = {'Apple': 50, 'Banana': 30, 'Mango': 75, 'Grapes': 100}  
series = pd.Series(data)  

print(series)

Output:

Apple      50  
Banana     30  
Mango      75  
Grapes    100  
dtype: int64  

🔹 The dictionary keys automatically become the index, making it easy to work with labeled data.


Key Takeaways:

Series is a one-dimensional labeled array in Pandas.
✅ It can be created from lists, NumPy arrays, and dictionaries.
✅ By default, Pandas assigns a numeric index (0,1,2,…) unless a custom index is specified.
✅ Using dictionary-based Series makes it easy to work with labeled data.

Leave a Reply

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