Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›Pandas›Pandas Coding Challenges

Interview Prep

Pandas Coding Challenges

This lesson focuses on solving complex data manipulation problems using Pandas to prepare for technical interview environments. Mastering these challenges ensures you can efficiently bridge the gap between raw data structures and meaningful analytical insights. You should reach for these techniques whenever you need to optimize performance or handle intricate data transformation tasks under pressure.

Handling Missing Data Strategically

In technical interviews, you are often presented with messy datasets containing gaps that skew results. The challenge is not merely removing rows, but deciding whether to fill them based on the distribution of the data. When you call 'fillna', you must reason about whether the mean, median, or a specific constant maintains the integrity of the distribution. Dropping data is a destructive operation that can lead to bias, especially if the missingness is non-random. Using 'groupby' to fill missing values within specific categories—such as filling missing temperatures by the city they belong to—is a frequent expectation in high-level interviews. By understanding that 'transform' allows you to apply functions back to the original index after aggregation, you gain a powerful tool to impute values contextually, ensuring that your statistical analysis remains robust and representative of the underlying data population without introducing artificial noise into your final results.

import pandas as pd
import numpy as np

df = pd.DataFrame({'City': ['A', 'A', 'B', 'B'], 'Temp': [20, np.nan, 30, np.nan]})
# Use group-specific median to fill missing values, preserving category variance
df['Temp'] = df['Temp'].fillna(df.groupby('City')['Temp'].transform('median'))
print(df)

Efficient Vectorized Transformations

Interviewers often test your ability to avoid slow iteration by leveraging vectorization. The core principle is that operations on entire series or dataframes are pushed to compiled, optimized C code, which is orders of magnitude faster than iterating with loops. When you apply a function, understanding whether to use 'apply' or a vectorized string or arithmetic method is crucial. 'Apply' is essentially a loop hidden from view and should be your last resort. Instead, aim to compose operations using native Pandas methods or numpy-backed arithmetic. By using boolean masks for conditional logic, you can perform complex filtering and assignment without writing a single 'if' statement. This approach is highly expressive and keeps your code readable, maintainable, and significantly more efficient. Mastering the syntax of vectorization allows you to treat entire columns as single objects, which is the foundational design philosophy that enables Pandas to handle millions of rows with ease during intensive computation tasks.

import pandas as pd

df = pd.DataFrame({'Sales': [100, 200, 300, 400]})
# Vectorized calculation avoids slow row-wise iteration
df['Bonus'] = df['Sales'].where(df['Sales'] > 250, 0) * 0.1
print(df)

Advanced Multi-Level Aggregation

Interview questions frequently demand grouping by multiple criteria to reveal hidden patterns. The 'groupby' method is not just for simple averages; it is a split-apply-combine framework. When you group by multiple columns, Pandas creates a hierarchical index (MultiIndex), which requires specific handling for post-aggregation selection. Understanding how to use the 'agg' method with a dictionary allows you to perform different operations on different columns simultaneously, such as taking the sum of revenue while calculating the mean of quantity. This is a common requirement when you need a summary report with varying statistical needs per metric. The power of this approach lies in its ability to collapse data into a multidimensional view that summarizes massive datasets into digestible insights. You must grasp the distinction between 'aggregate' and 'transform' here; the former reduces the dimensionality, while the latter preserves the original shape, which is often required for normalizing data against a grouped statistic.

import pandas as pd

df = pd.DataFrame({'Dept': ['HR', 'HR', 'IT', 'IT'], 'Year': [2021, 2022, 2021, 2022], 'Val': [10, 20, 30, 40]})
# Simultaneous multi-metric aggregation using a dictionary mapping
summary = df.groupby(['Dept', 'Year']).agg({'Val': ['sum', 'mean']})
print(summary)

Relational Data Merging

Data rarely lives in a single source, and being able to join disparate datasets is a staple of technical interviews. You must master the different types of joins—inner, left, right, and outer—and understand how they affect the resulting record count. A common mistake is losing data through an inner join when an outer join was required to preserve all records. When joining on multiple keys, Pandas allows you to specify a list of column names, ensuring the intersection is granular enough to maintain logical integrity. Furthermore, you should be comfortable using 'indicator=True' to debug merge mismatches, which helps you identify rows that failed to match. Understanding how the index versus specific columns behave during a merge is also vital. Always verify the uniqueness of your keys before merging to avoid accidental Cartesian products, which can explode your dataframe size and cause significant performance bottlenecks in your memory-constrained environment.

import pandas as pd

df1 = pd.DataFrame({'ID': [1, 2], 'Val': ['A', 'B']})
df2 = pd.DataFrame({'ID': [2, 3], 'Score': [80, 90]})
# Outer merge preserves all data from both sides, identifying missing matches
merged = pd.merge(df1, df2, on='ID', how='outer', indicator=True)
print(merged)

Time Series Resampling Techniques

Time series analysis is a frequent topic in technical assessments because it requires careful handling of indices. Pandas provides the 'resample' method specifically for converting frequency, such as moving from daily data to monthly averages. This is fundamentally different from 'groupby' because it accounts for the temporal spacing of the data points. You must understand 'up-sampling', where you increase frequency, and 'down-sampling', where you collapse data. When down-sampling, you must define an aggregation strategy, such as taking the 'last' value or the 'sum' for the period. Missing time intervals are handled gracefully by filling gaps using methods like forward fill or interpolation. These techniques ensure your time series remains continuous, which is necessary for valid forecasting and trend analysis. Knowing how to manipulate dates and use time-aware offsets allows you to solve complex temporal puzzles that occur when aligning different business reporting cycles or financial events within a dataframe.

import pandas as pd

dates = pd.date_range('2023-01-01', periods=5, freq='D')
df = pd.DataFrame({'Val': [1, 2, 3, 4, 5]}, index=dates)
# Resample daily data to weekly, taking the mean of each week
weekly = df.resample('W').mean()
print(weekly)

Key points

  • Always prefer vectorized operations over explicit loops for performance.
  • The split-apply-combine pattern is essential for mastering complex data aggregations.
  • Understand the difference between inner, outer, and left merges to avoid data loss.
  • Imputing missing values requires a contextual approach based on data distribution.
  • MultiIndex objects allow for sophisticated hierarchical data representation and analysis.
  • Resampling is the primary tool for manipulating time-series frequency effectively.
  • Use 'indicator=True' in merges to troubleshoot data alignment issues quickly.
  • Vectorization relies on pushing computational logic to optimized backend code structures.

Common mistakes

  • Mistake: Modifying a DataFrame in place without using inplace=True or reassignment. Why it's wrong: Many pandas methods return a new object rather than modifying the original. Fix: Assign the result back to the variable (df = df.dropna()) or use the inplace=True parameter where supported.
  • Mistake: Iterating over rows using a for-loop. Why it's wrong: It negates the performance benefits of vectorized operations. Fix: Use vectorization, map, apply, or list comprehensions if absolutely necessary.
  • Mistake: Using chained indexing (df['col']['row']) to set values. Why it's wrong: It can lead to the 'SettingWithCopyWarning' because pandas cannot guarantee if the result is a view or a copy. Fix: Use .loc[row, col] for label-based indexing or .iloc for position-based indexing.
  • Mistake: Comparing NaN values using equality (==). Why it's wrong: NaN is defined as not equal to itself according to IEEE floating-point standards. Fix: Use the .isna() or .notna() methods instead of comparing to np.nan.
  • Mistake: Forgetting to reset the index after filtering or grouping. Why it's wrong: The original index labels are preserved, which can cause alignment errors in subsequent joins or plotting. Fix: Use .reset_index(drop=True) to create a clean range index.

Interview questions

How do you select specific columns and filter rows based on a condition in Pandas?

To select columns and filter rows, you use square bracket notation or the .loc accessor. For example, if you have a DataFrame 'df', you can filter using 'df[df['age'] > 30][['name', 'city']]'. This approach is preferred because it is explicit and readable. Filtering first reduces the memory footprint of the intermediate object, and selecting specific columns ensures you are not performing operations on unnecessary data, which is critical for performance when dealing with large datasets.

What is the difference between 'map', 'apply', and 'applymap' in Pandas, and when should you use each?

These methods serve different scopes of application. 'map' is used for Series to substitute values based on a dictionary or function. 'apply' works on both Series and DataFrames; on a DataFrame, it operates column-wise or row-wise. 'applymap' is exclusively for DataFrames and applies a function to every single element. You should use 'map' for element-wise transformations on a single column, 'apply' for complex row/column logic, and 'applymap' only when element-wise transformation across the entire DataFrame is strictly necessary, as it is computationally expensive.

Compare the performance and usage of 'merge' versus 'concat' for combining datasets.

The 'merge' function is designed for relational database-style joins, such as inner, outer, left, or right joins on common keys. It is computationally more complex because it involves aligning keys. In contrast, 'concat' is used for stacking DataFrames either vertically or horizontally along an axis. You should use 'concat' when you have data with identical structures that you want to stack, as it is much faster. Use 'merge' only when you need to perform relational lookups based on matching values in specific columns.

How do you handle missing values, and why might you choose 'ffill' over a simple mean imputation?

Handling missing data depends on the nature of the dataset. You can drop rows with '.dropna()' or fill them with '.fillna()'. Choosing 'ffill' (forward fill) is ideal for time-series data where the last known value is likely the most accurate proxy for the current state. Conversely, mean imputation can artificially reduce the variance of your data and mask outliers, which can lead to biased model training. Using 'ffill' maintains the temporal continuity of the data, whereas mean imputation assumes a stationary distribution that may not exist.

How can you efficiently perform group-wise operations using 'groupby' and 'transform'?

The 'groupby' method followed by 'transform' is powerful because it allows you to perform aggregation while maintaining the original shape of your DataFrame. For example, 'df['score'] / df.groupby('group')['score'].transform('mean')' calculates a ratio relative to the group mean for every row. Unlike 'aggregate', which collapses rows into a single summary entry, 'transform' returns a Series aligned with the original index. This is essential for feature engineering, such as calculating group-relative deviations without losing individual row data or needing to perform complex merges later.

Explain the concept of 'vectorization' in Pandas and why it is superior to iterating over a DataFrame with a for-loop.

Vectorization is the process of executing operations on entire arrays at once rather than row-by-row. When you use Pandas built-in operations like 'df['a'] + df['b']', you are triggering highly optimized C-code underlying the library. Iterating with a for-loop, such as using 'iterrows()', is extremely slow because it introduces high Python overhead for every single operation. Vectorization allows the CPU to use SIMD instructions to process data in parallel, resulting in speed improvements that can be several orders of magnitude faster than standard procedural loops in complex datasets.

All Pandas interview questions →

Check yourself

1. You have a DataFrame 'df'. Which approach is the most efficient way to multiply every value in a numeric column by 2?

  • A.df['col'] = df['col'].apply(lambda x: x * 2)
  • B.for i in range(len(df)): df.loc[i, 'col'] *= 2
  • C.df['col'] = df['col'] * 2
  • D.df['col'].transform(lambda x: x * 2)
Show answer

C. df['col'] = df['col'] * 2
Vectorized operations (option 3) are implemented in C and are significantly faster than .apply() (option 1) or loop-based methods (option 2). .transform() is useful for broadcasting grouped results but overkill here.

2. What happens when you perform 'df1 + df2' where df1 and df2 have different sets of index labels?

  • A.It raises a KeyError
  • B.It performs an outer join, resulting in NaNs where labels do not overlap
  • C.It ignores the index and performs element-wise addition based on position
  • D.It truncates the result to the intersection of the two indices
Show answer

B. It performs an outer join, resulting in NaNs where labels do not overlap
Pandas aligns data by index and column labels during binary operations. If a label is missing in one DataFrame, it defaults to NaN (outer join behavior). It does not raise an error, nor does it ignore labels.

3. Which of the following is the correct way to select rows where 'Age' is greater than 30 and 'City' is 'New York'?

  • A.df.loc[df['Age'] > 30 and df['City'] == 'New York']
  • B.df.loc[(df['Age'] > 30) & (df['City'] == 'New York')]
  • C.df[(df['Age'] > 30) and (df['City'] == 'New York')]
  • D.df.select(Age > 30, City == 'New York')
Show answer

B. df.loc[(df['Age'] > 30) & (df['City'] == 'New York')]
Pandas requires bitwise operators (&, |) for boolean indexing because standard Python 'and'/'or' keywords cannot be overloaded for Series. Additionally, parentheses are required to enforce operator precedence.

4. What is the primary difference between .loc and .iloc?

  • A..loc is for labels, while .iloc is for integer-based positional indexing
  • B..loc is for rows, while .iloc is for columns
  • C..loc is always faster than .iloc
  • D..loc supports boolean indexing, but .iloc does not
Show answer

A. .loc is for labels, while .iloc is for integer-based positional indexing
.loc is label-based, meaning you use the names of the index/columns. .iloc is purely integer-based (0 to n-1). Both support boolean indexing and slicing, so the other options are incorrect.

5. When grouping data, what does the .agg() method allow you to do that .groupby().sum() does not?

  • A.It allows calculating the sum of the group only
  • B.It prevents the grouping index from becoming a column
  • C.It allows applying different aggregation functions to different columns simultaneously
  • D.It automatically removes all non-numeric columns from the output
Show answer

C. It allows applying different aggregation functions to different columns simultaneously
.agg() provides flexibility to pass a dictionary of columns and their corresponding aggregation functions (e.g., {'A': 'sum', 'B': 'mean'}). .groupby().sum() applies the same function to all selected columns.

Take the full Pandas quiz →

← PreviousPandas Interview Questions — Advanced

Pandas

34 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app