Bird
Raised Fist0
Pandasdata~5 mins

Creating Series from list and dictionary in Pandas

Choose your learning style10 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.

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