Element-wise DataFrame Formatting
Advanced · Write the code · from Pandas apply(), map(), transform(): Custom Data Transformations
From the lesson: Pandas apply(), map(), transform(): Custom Data Transformations
Pandas gives you dozens of built-in methods like .sum(), .mean(), and .str.upper(). But what happens when you need to run your own custom logic on every row, column, or cell? That is where apply(), map(), and transform() come in.
Read the lessonUse DataFrame.map() to format every numeric cell in df as a string with a dollar sign and 2 decimal places (e.g., "$10.50").
Print the first row as a list: print(df.iloc[0].tolist()).
import pandas as pd
df = pd.DataFrame({
'Q1': [10.5, 20.0],
'Q2': [15.75, 30.25]
})
# Format every cell as a dollar string
More Pandas Apply and Transform exercises
- Map Values with a DictionaryIntermediate
- Row-Level ApplyAdvanced
- Predict the map() OutputIntermediate
- Refactor apply() to VectorizedAdvanced
- Fix the apply() BugAdvanced
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz