Pandas Creating and Accessing Series – Build & Explore 1D Labeled Data
Introduction – Why Learn Series Creation and Access?
A Pandas Series is the foundation of data handling in Pandas. Before moving to complex DataFrames or transformations, you must master how to create, access, and interact with Series objects effectively.
In this guide, you’ll learn:
- Various methods to create a Series in Pandas
- How to assign custom indexes to Series
- Different ways to access elements using labels and positions
- Perform slicing, conditional access, and indexing
1. Create a Pandas Series from a List
import pandas as pd
data = [10, 20, 30, 40]
series = pd.Series(data)
print(series)
Output:
0 10
1 20
2 30
3 40
dtype: int64
A default integer index (0 to n-1) is assigned.
2. Create Series with Custom Index
series = pd.Series([100, 200, 300], index=['a', 'b', 'c'])
print(series)
Output:
a 100
b 200
c 300
dtype: int64
Index can be strings, numbers, or even datetime objects.
3. Create Series from a Dictionary
data = {'apple': 3, 'banana': 5, 'orange': 2}
series = pd.Series(data)
print(series)
Output:
apple 3
banana 5
orange 2
dtype: int64
Keys become index labels, and values become data.
4. Create Series with Scalar Value
series = pd.Series(7, index=['x', 'y', 'z'])
print(series)
Output:
x 7
y 7
z 7
dtype: int64
A scalar is broadcasted across all index values.
5. Access Elements by Position (.iloc)
print(series.iloc[0]) # First element
print(series.iloc[-1]) # Last element
.iloc[] is used for integer-based indexing.
6. Access Elements by Label (.loc)
print(series.loc['x']) # Access using index label
.loc[] is used for label-based indexing.
7. Slice a Series
print(series[1:3]) # Slicing by position
print(series['x':'y']) # Slicing by label (inclusive)
Label slicing in Series is inclusive of end index.
8. Conditional Access
print(series[series > 6])
Output:
x 7
y 7
z 7
dtype: int64
Return only values matching the condition.
9. Useful Series Attributes & Methods
| Attribute / Method | Purpose |
|---|---|
series.index | Return the index object |
series.values | Return values as NumPy array |
series.dtype | Data type of the Series |
series.head() | First 5 elements |
series.tail() | Last 5 elements |
Summary – Recap & Next Steps
Creating and accessing Pandas Series is an essential step before moving on to complex analysis. With just a few lines, you can build and interact with labeled data for real-world tasks like filtering, preprocessing, and feature extraction.
Key Takeaways:
- Series can be created from lists, dictionaries, and scalar values
- You can use custom or default indexing
- Access data using
.iloc[],.loc[], or slicing - Series supports conditional and vectorized access
Real-world relevance: Common in preprocessing steps like creating target vectors, handling labels, and managing 1D data columns.
FAQs – Creating and Accessing Series
Can a Series have duplicate index labels?
Yes, but accessing such labels returns multiple values.
How do I change the index of a Series?
Use:
series.index = ['a', 'b', 'c']
What happens if I access a missing label using .loc[]?
It will raise a KeyError.
Can I slice Series with non-integer indexes?
Yes, if labels are sorted. Label slicing is inclusive.
Are Series faster than lists?
Yes, due to underlying NumPy optimization.
Share Now :
