Product

Thursday, 19 March 2026

https://machinelearningmastery.com/blog/page/19/

 

7 Pandas Tricks That Cut Your Data Prep Time in Half

7 Pandas Tricks That Cut Your Data Prep Time in Half
Image by Editor | ChatGPT

Data preparation is one of the most time-consuming parts of any data science or analytics project, but it doesn’t have to be. With the proper techniques, Pandas can help you quickly transform messy and complex datasets into clean, ready-to-analyze formats. From handling missing data to reshaping and optimizing your DataFrames, a few tricks can save you hours of work.

In this article, you will discover seven practical Pandas tips that can speed up your data prep process and help you focus more on analysis and less on cleanup.

1. Chain Transformations with assign()

Creating new columns or modifying existing ones is a core part of data transformation. Instead of creating intermediate variables or breaking the process into multiple steps, .assign() method lets you chain transformations.

Here, we create a new column total_sales by multiplying units_sold with unit_price, and then create another column log_sales with the log of total sales. 

2. Fill Missing Values with a Dict in fillna()

Handling missing values is one of the first and most frequent data prep tasks. Instead of writing individual fill statements for each column, you can fill multiple columns using fillna().

This approach gives you full control over how each column is filled. For example, you can replace missing prices with 0 and fill missing category labels with a string like “Unknown”.

3. Flatten List Columns with explode()

Many datasets (e.g., JSON, nested CSVs) include columns with list-like data. .explode() function is a tool for flattening those into individual rows while keeping other columns intact.

This function transforms each item in the list into its own row while keeping the rest of the data intact. It’s useful for handling one-to-many relationships where each row can relate to multiple tags.

4. Readable Filtering with query()

Filtering data with logical conditions can get messy, especially when chaining multiple conditions. .query() offers a much more readable way to filter rows using SQL-like expressions.

This makes it easy to apply filters without deeply nested brackets. You can write complex conditions that are still easy to understand at a glance.

5. Named Aggregations with groupby().agg()

Summarizing your data is a crucial part of the analysis process. Instead of using default aggregation names, you can assign custom names to each metric using named aggregations in the .agg() method.

This way, your resulting DataFrame has meaningful column names like avg_sales and max_discount instead of just sales or discount.

6. Date Parsing with pd.to_datetime()

Dealing with messy date formats? Use pd.to_datetime() to convert strings to proper date objects. This makes it easy to work with date-based operations later.

Setting errors='coerce' tells Pandas to turn bad date strings into NaT (Not a Time), which avoids errors during parsing.

7. Modular Workflows with pipe()

As data transformations become increasingly complex, your code can become more difficult to follow. .pipe() helps you build modular, reusable pipelines by chaining custom functions.

By using this function, you pass the DataFrame through a sequence of custom functions. Each function takes the DataFrame as its first argument and returns a modified version.

Final Thoughts

With just a few Pandas tricks, you can drastically improve your data preparation speed and clarity. Here’s a quick recap:

With just a few Pandas tricks, you can drastically improve your data preparation speed and clarity. Here’s a quick recap.

These techniques help you reduce boilerplate, write clearer code, and get your data ready for analysis faster. By mastering them, you’ll spend less time cleaning and more time generating insights.

No comments:

Post a Comment

Connect broadband

AI:List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with trauma for kin ocd formed after forced to do rituals, family tribal values honor specific activities, utmost religious activities occult witchcraft enforced to do by elder sibling his wife and mother thrice with great celibacy turned the kundalini activated after deep celibacy penance shouting physical Mental financial Extortion by sibling with toxic behaviours and the wife mother and then mockery suppression asking about who’re your worth backbiting among relatives sector toon negligence of intelligence intuitive behaviour- all religious psychological traps when not work and lead to loss of consciousness of kin trance state recovered by governance with technocrats using AI - kin was asked by mother that he’s to go the male should not be seen in house he’s many absurd thought forcefully in brain mind and outsiders already feel awkward of this kin unresponsive behaviour due to TBI PTSD ocd cvt and tell at workplace social gathering he’s to go and asked for sector toon extortion and mockery in various sense due to un practical behaviour negligence of body building due to neglect ion if essential food nutrition and stubbed suppressed in every sense in religious psychological traps by family members AI humanoid using various neural networks and LLMs for kin and required steps.

  List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with tr...