🐍 Python Interview Question (Data Analyst)
Question : What is the difference between apply() and map() in Pandas?
Answer:
map() works on Series only and is used for element-wise transformations.
apply() works on Series as well as DataFrames and can apply a function row-wise or column-wise.
Example :
df['salary_lakhs'] = df['salary'].map(lambda x: x / 100000)
df['total'] = df.apply(lambda row: row['sales'] - row['cost'], axis=1)
👉 Interview Tip:
Use map() for simple value replacement or transformation.
Use apply() when logic depends on multiple columns.
👉 Follow the channel and react ❤️ to this post for more Python & Data Analyst interview questions, tips, and cheat sheets shared regularly 🚀
Post #1310
6K
- ❤ 14