September 19, 2024

Method Chaining in Python: A Technique for Elegant Coding

1 min read
rb_thumb

rbs-img

Pandas is a must-have data manipulation library for data scientists. One of its unfair advantages is the ability to use chainable methods for handling data:

import pandas as pd

# Sample data

data = {

‘Name’: [‘Yang Zhou’, ‘Jane’, ‘Mary’, ‘Jack’],

‘Age’: [30, 34, 58, 66],

‘City’: [‘London’, ‘Tokyo’, ‘Chicago’, ‘Tokyo’]

}

# Create a DataFrame

df = pd.DataFrame(data)

# Use chained methods to clean data and compute statistics

result = (df

.query(‘Age >= 30’) # Filter rows where Age is greater than 30

.groupby(‘City’) # Group by city

.agg(Average_Age=(‘Age’, ‘mean’)) # Compute the average age per city

.reset_index() # Reset index to turn ‘City’ back into a column

)

print(result)

# City Average_Age

# 0 Chicago 58.0

# 1 London 30.0

# 2 Tokyo 50.0

As the above code snippet shows, we are able to call four methods continuously in a sequence and get our final result. This technique is formally named “method chaining”.

It is a technique in which methods are called in succession, each operating on the result of the preceding one without the need for intermediate variables.

Method chaining not only makes our code concise but also enhances readability by clearly laying out the sequence of operations.

If we re-write the above code without method chaining, it will work as well, but there is a lack of elegance and fluidity:

Source: medium.com

Leave a Reply

Your email address will not be published. Required fields are marked *