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.
Creating Series from list and dictionary in 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.
import pandas as pd # Example 1: From list without index numbers = [5, 10, 15] series1 = pd.Series(numbers) print(series1)
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)
import pandas as pd # Example 3: From dictionary scores = {'math': 90, 'science': 85, 'english': 88} series3 = pd.Series(scores) print(series3)
import pandas as pd # Example 4: Empty list empty_list = [] series4 = pd.Series(empty_list) print(series4)
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.
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)
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.
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.