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.
Practice
Solution
Step 1: Understand Series creation from dictionary
When creating a Series from a dictionary, pandas uses the dictionary keys as the index labels.Step 2: Identify the role of dictionary keys and values
The dictionary values become the data in the Series, and keys become the index labels automatically.Final Answer:
The dictionary keys become the Series index labels automatically. -> Option AQuick Check:
Dictionary keys = index labels [OK]
- Confusing keys with values as index
- Thinking Series always uses numeric index
- Assuming error occurs with dict input
[10, 20, 30] with index labels ['a', 'b', 'c']?Solution
Step 1: Recall Series constructor parameters
The correct parameters are data first, then index for labels.Step 2: Match correct syntax
pd.Series([10, 20, 30], index=['a', 'b', 'c']) usespd.Series([10, 20, 30], index=['a', 'b', 'c'])which is correct syntax.Final Answer:
pd.Series([10, 20, 30], index=['a', 'b', 'c']) -> Option AQuick Check:
Use index= for labels in Series constructor [OK]
- Using labels= instead of index=
- Swapping data and index parameters
- Passing index before data
import pandas as pd
s = pd.Series({'x': 5, 'y': 10, 'z': 15})
print(s['y'])Solution
Step 1: Create Series from dictionary
The Series has index labels 'x', 'y', 'z' with values 5, 10, 15 respectively.Step 2: Access value by index label 'y'
Accessing s['y'] returns the value 10.Final Answer:
10 -> Option BQuick Check:
s['y'] = 10 [OK]
- Confusing index label with position
- Expecting numeric index access
- Mistaking value for key
import pandas as pd s = pd.Series([1, 2, 3], index=['a', 'b'])
Solution
Step 1: Check data and index lengths
Data has 3 elements but index has only 2 labels.Step 2: Understand pandas behavior
Pandas raises ValueError if index length does not match data length.Final Answer:
Index length does not match data length causing ValueError. -> Option DQuick Check:
Data length != index length causes ValueError [OK]
- Assuming pandas auto-adjusts index length
- Confusing ValueError with SyntaxError
- Thinking index must be integers
{'a': 1, 'b': 0, 'c': 3, 'd': 0}. How do you create a pandas Series from it that only includes keys with values greater than zero?Solution
Step 1: Filter dictionary items with values > 0
Use dictionary comprehension to keep only items where value > 0.Step 2: Create Series from filtered dictionary
Pass the filtered dictionary to pd.Series to create the Series with desired keys and values.Final Answer:
pd.Series({k: v for k, v in d.items() if v > 0}) -> Option CQuick Check:
Filter dict first, then create Series [OK]
- Using filter() which is not a Series method
- Using dropna() which removes NaNs, not zeros
- Trying to index Series with dict directly
Ready to prove it?
Pass the test and this pattern moves unknown
