0
0
Data Analysis Pythondata~5 mins

Series creation from lists and dicts in Data Analysis Python

Choose your learning style9 modes available
Introduction

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

You have a list of numbers and want to add labels to each number.
You have data stored in a dictionary and want to convert it into a Series for analysis.
You want to quickly create a labeled column of data for a table.
You want to perform calculations on data with easy access by labels.
Syntax
Data Analysis Python
import pandas as pd

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

# Create 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, default labels 0, 1, 2... are used.

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

Examples
This creates an empty Series with no data and no labels.
Data Analysis Python
import pandas as pd

# Empty list
empty_list = []
series_empty = pd.Series(empty_list)
print(series_empty)
Series with one value and a custom label.
Data Analysis Python
import pandas as pd

# List with one element
single_element_list = [42]
series_single = pd.Series(single_element_list, index=['only'])
print(series_single)
Series created from a dictionary with one item.
Data Analysis Python
import pandas as pd

# Dictionary with one key-value pair
dict_single = {'x': 99}
series_single_dict = pd.Series(dict_single)
print(series_single_dict)
Series created from a list without specifying labels. Default numeric labels are used.
Data Analysis Python
import pandas as pd

# List without index
data_list = [5, 10, 15]
series_default_index = pd.Series(data_list)
print(series_default_index)
Sample Program

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

Data Analysis Python
import pandas as pd

# Create Series from list with labels
numbers_list = [100, 200, 300]
labels_list = ['first', 'second', 'third']
series_from_list = pd.Series(numbers_list, index=labels_list)

print('Series from list:')
print(series_from_list)

# Create Series from dictionary
dict_data = {'apple': 5, 'banana': 3, 'cherry': 7}
series_from_dict = pd.Series(dict_data)

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

Creating a Series from a list or dictionary is very fast, with time complexity O(n) where n is the number of elements.

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

A common mistake is forgetting to provide an index when creating from a list, which results in default numeric labels that might not be meaningful.

Use dictionary input when your data already has meaningful labels. Use list input when you want to assign labels yourself or use default numeric labels.

Summary

A Series stores data with labels for easy access and analysis.

You can create a Series from a list by optionally giving labels with the index parameter.

Creating a Series from a dictionary automatically uses the dictionary keys as labels.