Bird
Raised Fist0
Pandasdata~10 mins

Creating Series from list and dictionary in Pandas - Visual Walkthrough

Choose your learning style10 modes available
Concept Flow - Creating Series from list and dictionary
Start with data: list or dict
↓
Call pd.Series(data)
↓
If list: create Series with default index
↓
If dict: create Series with keys as index
↓
Result: Series object with values and index
↓
Done
We start with a list or dictionary, then call pandas Series constructor. If input is a list, Series gets default numeric index. If input is a dictionary, Series uses keys as index.
Execution Sample
Pandas
import pandas as pd

lst = [10, 20, 30]
ser_list = pd.Series(lst)

dict_data = {'a': 100, 'b': 200}
ser_dict = pd.Series(dict_data)
Create two Series: one from a list with default index, one from a dictionary with keys as index.
Execution Table
StepInput TypeInput DataActionOutput Series (values and index)
1list[10, 20, 30]Call pd.Series(lst)0:10, 1:20, 2:30
2dict{'a': 100, 'b': 200}Call pd.Series(dict_data)a:100, b:200
3--End of creationSeries objects ready
💡 All inputs converted to Series; list uses default numeric index, dict uses keys as index.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
lst[10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30]
ser_listnullSeries(values=[10, 20, 30], index=[0, 1, 2])Series(values=[10, 20, 30], index=[0, 1, 2])Series(values=[10, 20, 30], index=[0, 1, 2])
dict_data{'a': 100, 'b': 200}{'a': 100, 'b': 200}{'a': 100, 'b': 200}{'a': 100, 'b': 200}
ser_dictnullnullSeries(values=[100, 200], index=['a', 'b'])Series(values=[100, 200], index=['a', 'b'])
Key Moments - 3 Insights
Why does the Series created from a list have numeric index starting at 0?
Because when you create a Series from a list, pandas automatically assigns a default integer index starting from 0, as shown in execution_table step 1.
Why does the Series created from a dictionary use the dictionary keys as the index?
When creating a Series from a dictionary, pandas uses the dictionary keys as the index labels, as seen in execution_table step 2.
Can the index be customized when creating a Series from a list or dictionary?
Yes, you can pass an index parameter to pd.Series to set custom index labels, but in this example default behavior is shown.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 1. What is the index of the Series created from the list?
A[10, 20, 30]
B['a', 'b', 'c']
C[0, 1, 2]
D['x', 'y', 'z']
💡 Hint
Check the 'Output Series' column in execution_table row 1 for index values.
According to variable_tracker, what is the value of ser_dict after step 2?
ASeries with values [10, 20, 30] and index [0,1,2]
BSeries with values [100, 200] and index ['a', 'b']
Cnull
DDictionary {'a': 100, 'b': 200}
💡 Hint
Look at the 'ser_dict' row and 'After Step 2' column in variable_tracker.
If we create a Series from a dictionary with keys ['x', 'y'], what will be the index?
A['x', 'y']
B[0, 1]
C['a', 'b']
DDefault numeric index starting at 0
💡 Hint
Refer to the concept_flow and execution_table step 2 where dictionary keys become index.
Concept Snapshot
pd.Series(data) creates a Series from list or dict.
- From list: values get default numeric index starting at 0.
- From dict: keys become index labels.
- Result is a one-dimensional labeled array.
- Index can be customized with index= parameter.
Full Transcript
This visual execution shows how to create pandas Series from a list and a dictionary. Starting with a list of numbers, calling pd.Series creates a Series with default numeric index 0,1,2. Starting with a dictionary, pd.Series uses the dictionary keys as the index labels. The execution table traces each step, showing input type, data, action, and resulting Series values and index. The variable tracker shows how variables change after each step. Key moments clarify why list-based Series have numeric index and dictionary-based Series use keys as index. The visual quiz tests understanding of index assignment and variable states. The concept snapshot summarizes the syntax and behavior for quick reference.

Practice

(1/5)
1. What happens when you create a pandas Series from a dictionary?
easy
A. The dictionary keys become the Series index labels automatically.
B. The dictionary values become the index labels.
C. The Series will have numeric index ignoring the dictionary keys.
D. It raises an error because Series cannot be created from dictionaries.

Solution

  1. Step 1: Understand Series creation from dictionary

    When creating a Series from a dictionary, pandas uses the dictionary keys as the index labels.
  2. 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.
  3. Final Answer:

    The dictionary keys become the Series index labels automatically. -> Option A
  4. Quick Check:

    Dictionary keys = index labels [OK]
Hint: Keys become index labels automatically when using dict [OK]
Common Mistakes:
  • Confusing keys with values as index
  • Thinking Series always uses numeric index
  • Assuming error occurs with dict input
2. Which of the following is the correct way to create a pandas Series from a list [10, 20, 30] with index labels ['a', 'b', 'c']?
easy
A. pd.Series([10, 20, 30], index=['a', 'b', 'c'])
B. pd.Series(index=['a', 'b', 'c'], data=[10, 20, 30])
C. pd.Series([10, 20, 30], labels=['a', 'b', 'c'])
D. pd.Series(data=[10, 20, 30], labels=['a', 'b', 'c'])

Solution

  1. Step 1: Recall Series constructor parameters

    The correct parameters are data first, then index for labels.
  2. Step 2: Match correct syntax

    pd.Series([10, 20, 30], index=['a', 'b', 'c']) uses pd.Series([10, 20, 30], index=['a', 'b', 'c']) which is correct syntax.
  3. Final Answer:

    pd.Series([10, 20, 30], index=['a', 'b', 'c']) -> Option A
  4. Quick Check:

    Use index= for labels in Series constructor [OK]
Hint: Use index= for labels when creating Series from list [OK]
Common Mistakes:
  • Using labels= instead of index=
  • Swapping data and index parameters
  • Passing index before data
3. What is the output of this code?
import pandas as pd
s = pd.Series({'x': 5, 'y': 10, 'z': 15})
print(s['y'])
medium
A. 5
B. 10
C. 15
D. KeyError

Solution

  1. Step 1: Create Series from dictionary

    The Series has index labels 'x', 'y', 'z' with values 5, 10, 15 respectively.
  2. Step 2: Access value by index label 'y'

    Accessing s['y'] returns the value 10.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    s['y'] = 10 [OK]
Hint: Access Series by index label to get corresponding value [OK]
Common Mistakes:
  • Confusing index label with position
  • Expecting numeric index access
  • Mistaking value for key
4. Identify the error in this code snippet:
import pandas as pd
s = pd.Series([1, 2, 3], index=['a', 'b'])
medium
A. TypeError because index must be integers.
B. SyntaxError due to missing parentheses.
C. No error; code runs fine.
D. Index length does not match data length causing ValueError.

Solution

  1. Step 1: Check data and index lengths

    Data has 3 elements but index has only 2 labels.
  2. Step 2: Understand pandas behavior

    Pandas raises ValueError if index length does not match data length.
  3. Final Answer:

    Index length does not match data length causing ValueError. -> Option D
  4. Quick Check:

    Data length != index length causes ValueError [OK]
Hint: Ensure index length matches data length when creating Series [OK]
Common Mistakes:
  • Assuming pandas auto-adjusts index length
  • Confusing ValueError with SyntaxError
  • Thinking index must be integers
5. You have a dictionary {'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?
hard
A. pd.Series(d).loc[d > 0]
B. pd.Series(d).filter(lambda x: x > 0)
C. pd.Series({k: v for k, v in d.items() if v > 0})
D. pd.Series(d).dropna()

Solution

  1. Step 1: Filter dictionary items with values > 0

    Use dictionary comprehension to keep only items where value > 0.
  2. Step 2: Create Series from filtered dictionary

    Pass the filtered dictionary to pd.Series to create the Series with desired keys and values.
  3. Final Answer:

    pd.Series({k: v for k, v in d.items() if v > 0}) -> Option C
  4. Quick Check:

    Filter dict first, then create Series [OK]
Hint: Filter dict before Series creation to exclude unwanted keys [OK]
Common Mistakes:
  • Using filter() which is not a Series method
  • Using dropna() which removes NaNs, not zeros
  • Trying to index Series with dict directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard