0
0
Pandasdata~5 mins

Creating Series from list and dictionary in Pandas

Choose your learning style9 modes available
Introduction

A Series is a simple way to store data with labels. Creating a Series from a list or dictionary helps organize data so you can analyze it easily.

You have a list of numbers or names and want to add labels to each item.
You have data stored as key-value pairs and want to convert it into a table-like structure.
You want to perform calculations or filtering on a simple labeled dataset.
You want to prepare data for plotting or further analysis.
You want to quickly check or summarize data stored in lists or dictionaries.
Syntax
Pandas
import pandas as pd

# Creating Series from a list
data_list = [10, 20, 30]
series_from_list = pd.Series(data_list, index=['a', 'b', 'c'])

# Creating Series from a dictionary
data_dict = {'a': 10, 'b': 20, 'c': 30}
series_from_dict = pd.Series(data_dict)

The index parameter is optional when creating from a list. If not given, pandas uses numbers starting from 0.

When creating from a dictionary, the keys become the index labels automatically.

Examples
This creates a Series with default numeric index starting at 0.
Pandas
import pandas as pd

# Example 1: From list without index
numbers = [5, 10, 15]
series1 = pd.Series(numbers)
print(series1)
Here, we assign custom labels 'x', 'y', 'z' to each fruit.
Pandas
import pandas as pd

# Example 2: From list with custom index
fruits = ['apple', 'banana', 'cherry']
series2 = pd.Series(fruits, index=['x', 'y', 'z'])
print(series2)
The keys become the index labels automatically.
Pandas
import pandas as pd

# Example 3: From dictionary
scores = {'math': 90, 'science': 85, 'english': 88}
series3 = pd.Series(scores)
print(series3)
Creating a Series from an empty list results in an empty Series.
Pandas
import pandas as pd

# Example 4: Empty list
empty_list = []
series4 = pd.Series(empty_list)
print(series4)
Sample Program

This program shows how to create Series from both a list with custom labels and from a dictionary. It prints both Series so you can see they look the same.

Pandas
import pandas as pd

# Create Series from list
prices_list = [100, 200, 300]
labels_list = ['item1', 'item2', 'item3']
series_prices = pd.Series(prices_list, index=labels_list)

print('Series created from list:')
print(series_prices)

# Create Series from dictionary
prices_dict = {'item1': 100, 'item2': 200, 'item3': 300}
series_prices_dict = pd.Series(prices_dict)

print('\nSeries created from dictionary:')
print(series_prices_dict)
OutputSuccess
Important Notes

Creating a Series from a list is simple but you must provide an index if you want labels other than numbers.

Creating from a dictionary automatically uses keys as labels, which is very convenient.

Time complexity is O(n) where n is the number of elements, as pandas copies data into the Series.

Space complexity is O(n) because the Series stores all data and labels.

A common mistake is forgetting to provide an index when needed, which leads to default numeric labels that may confuse analysis.

Use Series from dictionary when you already have labeled data; use from list when you have ordered data without labels.

Summary

A pandas Series stores data with labels called an index.

You can create a Series from a list by optionally giving labels.

You can create a Series from a dictionary where keys become labels automatically.