Created with GPT 4o
“The goal is to turn data into information, and information into insight.” — Carly Fiorina
To do that, one of the best ways in data science is using Python with Pandas. But do we know each function in Pandas? I have been using it for years regularly, yet I still am discovering new functions.
What If I Told You There’s a Faster Way to Update Just One Cell?
Photo by Davor Denkovski on Unsplash
You don’t always need .loc — sometimes .at is way faster and simpler for single value edits. Let’s create a simple dataset.
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Score': [85, 90, 88]
})
Here is our dataframe.
SS of output
.at is the fastest way to update a single value in a DataFrame. It’s optimized for scalar access and avoids overhead from .loc.
Clean, fast, perfect for loops. Here is the code.
df.at[1, 'Score'] = 95
print(df)
Here is the output.
SS of output
As you can see, the score at index one has changed to 95. Simple and fast!
How to Split List Values into Separate Rows?
Photo by Drew Beamer on Unsplash
Sometimes one row hides many values. .explode() brings them to light. Here is our dataset creation code.
df = pd.DataFrame({
'Name': ['Alice', 'Bob'],
'Hobbies': [['Reading', 'Hiking'], ['Gaming']]
})
Here is our dataset.
SS of output
When your data includes lists in a column, .explode() helps flatten it into tidy rows. Great for normalization and analysis. Let’s see.
df = df.explode('Hobbies')
print(df)
Here is the output.
SS of output
If you have multiple values in your row, you know what to do!
Can You Control Your Data Like a Game? Yes — With .clip()
Photo by Erik Mclean on Unsplash
Instead of writing custom logic to cap values, just clip them. How? Let’s create a dataset at first.
df = pd.DataFrame({'Score': [45, 76, 101, -5]})
df
Here is our dataset.
SS of output
Now let’s control the data you have. Here is your code.
df['Score'] = df['Score'].clip(lower=0, upper=100)
df
Here is the output.
SS of output
.clip() cleanly enforces value boundaries without np.where or manual filtering. It’s your data’s safety net.
What If Filtering Felt Like Speaking?
Photo by Matthew Osborn on Unsplash
Tired of writing df[df[‘Age’] < 30]? Try .query() — it reads like plain English.
Here is our dataset creation code.
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
})
Here is our dataset.
SS of output
Let’s use a query and speak with the data.
young = df.query('Age < 30')
young
Here is the output.
SS of output
.query() is more readable, especially with multiple conditions. It’s expressive and closer to natural language or SQL.
How Many Columns Can You Transform at Once? More Than You Think
Photo by Yosep Surahman on Unsplash
Want to create new columns without breaking your flow? .assign() is your answer.
Here is the dataset creation code.
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
Here is our dataframe.
Let’s transform columns at once.
df = df.assign(sum=lambda x: x.a + x.b, mul=lambda x: x.a * x.b)
df
Here is the output.
SS of output
.assign() lets you create multiple new columns in one chain. It’s functional, composable, and more elegant than writing separate lines.
Final Thoughts
In this article, we have discovered five pandas tricks, with examples. And you saw how they enhanced the data analysis.
If you like what you saw, visit our platform, where you can see the effect of these techniques in AI projects, use our assistants to enhance the data analysis, especially Randy, who is good at this task. See you there!
Here are the free resources.
Here is the ChatGPT cheat sheet.
Here is the Prompt Techniques cheat sheet.
Here is my NumPy cheat sheet.
Here is the source code of the “How to be a Billionaire” data project.
Here is the source code of the “Classification Task with 6 Different Algorithms using Python” data project.
Here is the source code of the “Decision Tree in Energy Efficiency Analysis” data project.
Here is the source code of the “DataDrivenInvestor 2022 Articles Analysis” data project.
“Machine learning is the last invention that humanity will ever need to make.” Nick Bostrom