The Pandas Series is a data structure that combines the properties of both lists and dictionaries. Therefore, you can intuitively access individual data or rewrite values using defined indices (labels).
This article explains how to retrieve data by specifying indices, access data using attribute (dot) notation, and how to update data using these methods.
Methods for Referencing and Updating Data
There are two main ways to access elements within a Series:
- Bracket Notation
[]: Specifying the key (index) like a dictionary. This is the safest and most common method. - Dot Notation
.: Specifying the index name as an attribute of the object. This makes the code concise, but it cannot be used if the index name contains reserved words or spaces.
Below is the implementation code using weekly temperature data as an example.
import pandas as pd
def manage_temperature_data():
"""
Function to demonstrate referencing and updating data in Pandas Series
"""
# Create maximum temperature data for weekly weather forecast
# Specify days of the week in the index
temperatures = [24.5, 25.0, 23.8, 26.1]
days = ["Monday", "Tuesday", "Wednesday", "Thursday"]
series_temp = pd.Series(temperatures, index=days, name="Temperature")
print("--- Initial Data ---")
print(series_temp)
print("\n")
# 1. Referencing Data
print("=== Referencing Data ===")
# Access via Bracket Notation (Dictionary style)
# Specify the value using the string index as a key
temp_mon = series_temp["Monday"]
print(f"Temperature on Monday (Bracket Ref): {temp_mon}")
# Access via Dot Notation (Attribute style)
# Write the index name directly like a property
temp_tue = series_temp.Tuesday
print(f"Temperature on Tuesday (Dot Ref) : {temp_tue}")
print("\n")
# 2. Updating Data
print("=== Updating Data ===")
# Overwrite value using Bracket Notation
# Correcting Wednesday's temperature
series_temp["Wednesday"] = 28.5
print(f"Updated Wednesday: {series_temp['Wednesday']}")
# Overwrite value using Dot Notation
# Correcting Thursday's temperature
series_temp.Thursday = 27.0
print(f"Updated Thursday : {series_temp.Thursday}")
print("\n--- Data After Update ---")
print(series_temp)
if __name__ == "__main__":
manage_temperature_data()
Execution Result
--- Initial Data ---
Monday 24.5
Tuesday 25.0
Wednesday 23.8
Thursday 26.1
Name: Temperature, dtype: float64
=== Referencing Data ===
Temperature on Monday (Bracket Ref): 24.5
Temperature on Tuesday (Dot Ref) : 25.0
=== Updating Data ===
Updated Wednesday: 28.5
Updated Thursday : 27.0
--- Data After Update ---
Monday 24.5
Tuesday 25.0
Wednesday 28.5
Thursday 27.0
Name: Temperature, dtype: float64
Important Note: Limitations of Dot Notation
Dot notation (series.label) is easy to write and highly readable, but it is not all-purpose. You must use bracket notation (series["label"]) in the following cases:
- When the index name is not a valid Python identifier: For example, if it contains spaces (e.g.,
"New York") or starts with a number (e.g.,"1st_Day"). - When it overlaps with Series method names: If names like
sum,max,index, orvaluesare used as indices, using dot notation will refer to the method itself rather than the data.
Basically, it is common to use bracket notation, utilizing dot notation in situations where convenience is needed, such as interactive analysis.
