A Series is like a list with labels for each item. It helps you keep track of data with names, not just positions.
Series as labeled one-dimensional array in Pandas
import pandas as pd # Create a Series with data and optional labels (index) series = pd.Series(data, index=labels) # data: list, array, or scalar values # labels: list of labels for each data item (optional)
If you do not provide labels, pandas will use numbers starting from 0.
Labels can be strings, numbers, or other types, but they must be unique.
import pandas as pd # Example 1: Series with default numeric labels series1 = pd.Series([10, 20, 30]) print(series1)
import pandas as pd # Example 2: Series with custom string labels series2 = pd.Series([5, 15, 25], index=['a', 'b', 'c']) print(series2)
import pandas as pd # Example 3: Empty Series series3 = pd.Series([], dtype=float) print(series3)
import pandas as pd # Example 4: Series with one element and label series4 = pd.Series([100], index=['only']) print(series4)
This program creates a Series with fruit names as labels and quantities as data. It shows how to access, add, and remove items by label.
import pandas as pd # Create a Series with data and labels fruits = ['apple', 'banana', 'cherry'] quantities = [10, 20, 15] # Create Series with quantities and fruit names as labels fruit_series = pd.Series(quantities, index=fruits) print('Original Series:') print(fruit_series) # Access data by label print('\nQuantity of bananas:') print(fruit_series['banana']) # Add a new fruit fruit_series['orange'] = 12 print('\nSeries after adding orange:') print(fruit_series) # Remove a fruit fruit_series = fruit_series.drop('apple') print('\nSeries after removing apple:') print(fruit_series)
Time complexity for accessing or adding items by label is usually O(1) because Series uses a hash map internally.
Space complexity depends on the number of items stored in the Series.
A common mistake is confusing position-based access (like lists) with label-based access. Use series.iloc[] for position and series.loc[] for labels.
Use Series when you want labeled data that behaves like a one-dimensional array. Use DataFrame for two-dimensional labeled data.
A Series is a one-dimensional labeled array in pandas.
It stores data with labels (index) to access items by name.
Series supports easy data selection, addition, and removal by labels.
Practice
What is a pandas.Series best described as?
Solution
Step 1: Understand the definition of Series
A pandas Series is a one-dimensional array with labels called an index.Step 2: Compare options with definition
A one-dimensional labeled array that can hold any data type matches the definition exactly, others describe different pandas objects or unrelated concepts.Final Answer:
A one-dimensional labeled array that can hold any data type -> Option BQuick Check:
Series = one-dimensional labeled array [OK]
- Confusing Series with DataFrame (2D)
- Thinking Series is a function or method
- Mixing Series with file operations
Which of the following is the correct way to create a pandas.Series with values [10, 20, 30] and index labels ['a', 'b', 'c']?
import pandas as pd # Choose the correct code
Solution
Step 1: Recall Series constructor parameters
The Series constructor usesdataas first argument andindexfor labels.Step 2: Match options with correct parameter names
pd.Series([10, 20, 30], index=['a', 'b', 'c']) uses correct syntax: data list and index list. Others use wrong parameter names or order.Final Answer:
pd.Series([10, 20, 30], index=['a', 'b', 'c']) -> Option CQuick Check:
Use 'index' for labels in Series constructor [OK]
- Using 'labels' instead of 'index'
- Swapping data and index arguments
- Passing index as 'data' parameter
What will be the output of the following code?
import pandas as pd s = pd.Series([5, 10, 15], index=['x', 'y', 'z']) print(s['y'])
Solution
Step 1: Understand Series indexing by label
Series allows access by index label. Here, 'y' corresponds to value 10.Step 2: Check the printed value
Accessing s['y'] returns 10, so print outputs 10.Final Answer:
10 -> Option DQuick Check:
Access by label 'y' returns 10 [OK]
- Confusing label with value
- Expecting index position access with label
- Mistaking output for error
Identify the error in the following code snippet:
import pandas as pd s = pd.Series([1, 2, 3], index=['a', 'b', 'c']) print(s['d'])
Solution
Step 1: Check index labels in Series
The Series has index labels 'a', 'b', 'c'. 'd' is not present.Step 2: Understand what happens when accessing missing label
Accessing s['d'] raises KeyError because label 'd' does not exist.Final Answer:
KeyError because 'd' is not in index -> Option AQuick Check:
Accessing missing label causes KeyError [OK]
- Expecting default 0 or None for missing label
- Confusing KeyError with SyntaxError
- Thinking index labels must be integers
Given a pandas.Series s = pd.Series([100, 200, 300], index=['a', 'b', 'c']), how can you add a new element with label 'd' and value 400?
Solution
Step 1: Recall how to add elements to Series
You can add a new label and value by assignment using s[label] = value.Step 2: Check options for correct syntax
s['d'] = 400 uses s['d'] = 400 which adds new element. Others are invalid or incorrect methods.Final Answer:
s['d'] = 400 -> Option AQuick Check:
Assign new label with s['label'] = value [OK]
- Using non-existent methods like add or insert
- Trying to append with dict without reassignment
- Confusing Series with DataFrame methods
Ready to prove it?
Pass the test and this pattern moves unknown
