Data Science with Python Pandas DataFrame Operations Questions and Answers — Questions and Answers
Question 1: Given a Pandas DataFrame named `sales`, which of the following code snippets correctly calculates the total sales for each 'Region' by summing the 'Sales' column?
- sales.groupby('Region')['Sales'].sum() (Correct answer)
- sales.sum('Sales').by('Region')
- sales.aggregate('Sales').on('Region')
- sales.pivot(index='Region', values='Sales', aggfunc='sum')
Correct answer: sales.groupby('Region')['Sales'].sum()
The `groupby()` method is used to split the DataFrame into groups based on some criteria, in this case, the 'Region' column. Then, `['Sales']` selects the 'Sales' column from each group, and `.sum()` is an aggregation function that calculates the sum of the 'Sales' for each region.
Question 2: A data scientist has a DataFrame `df` with missing values in the 'Age' column. They want to fill these missing values with the mean age of all non-missing entries in that column. Which of the following is the correct way to achieve this in place?
- df['Age'].fill(df['Age'].mean())
- df['Age'].replace(np.nan, df['Age'].mean())
- df['Age'].fillna(df['Age'].mean(), inplace=True) (Correct answer)
- df.loc[df['Age'].isnull(), 'Age'] = df['Age'].mean(skipna=True)
Correct answer: df['Age'].fillna(df['Age'].mean(), inplace=True)
The `fillna()` method is the standard way to replace missing values (NaN) in a Pandas Series or DataFrame. The first argument is the value to use for filling, and setting `inplace=True` modifies the DataFrame directly without needing to reassign it.
Question 3: You have two DataFrames, `df1` and `df2`, with a common column 'employee_id'. You want to create a new DataFrame that contains only the rows where 'employee_id' exists in *both* `df1` and `df2`. Which merge operation should you use?
- pd.merge(df1, df2, on='employee_id', how='outer')
- pd.merge(df1, df2, on='employee_id', how='inner') (Correct answer)
- pd.merge(df1, df2, on='employee_id', how='left')
- pd.concat([df1, df2], on='employee_id')
Correct answer: pd.merge(df1, df2, on='employee_id', how='inner')
An 'inner' merge returns only the records that have matching keys in both DataFrames. This is equivalent to the intersection of the keys. An 'outer' merge would include all rows from both, a 'left' merge would include all rows from `df1` and matched rows from `df2`.
Question 4: What is the primary purpose of the `df.loc[]` accessor in a Pandas DataFrame?
- To select data by integer-based position.
- To perform complex mathematical computations.
- To select a single value using its row and column number.
- To select data by label-based indexing. (Correct answer)
Correct answer: To select data by label-based indexing.
The `.loc[]` accessor is used for label-based indexing, which means you can select rows and columns using their index labels and column names. In contrast, `.iloc[]` is used for integer-position based indexing.
Question 5: A data analyst is working with a DataFrame `market_data` and needs to create a new column called 'ProfitMargin'. This column should be calculated as `(market_data['Revenue'] - market_data['Cost']) / market_data['Revenue']`. Which of the following is the most idiomatic Pandas way to add this new column?
- market_data['ProfitMargin'] = (market_data['Revenue'] - market_data['Cost']) / market_data['Revenue'] (Correct answer)
- market_data.apply(lambda row: (row['Revenue'] - row['Cost']) / row['Revenue'], axis=1)
- for index, row in market_data.iterrows(): market_data.loc[index, 'ProfitMargin'] = (row['Revenue'] - row['Cost']) / row['Revenue']
- market_data.insert(column='ProfitMargin', value=(market_data.Revenue - market_data.Cost) / market_data.Revenue)
Correct answer: market_data['ProfitMargin'] = (market_data['Revenue'] - market_data['Cost']) / market_data['Revenue']
Pandas supports vectorized operations, which are highly efficient. Directly performing arithmetic operations on columns (which are Pandas Series) is the most direct and performant way to create a new column based on existing ones. Using `.apply()` or iterating with `iterrows()` is much less efficient for this type of calculation.
Question 6: Which of the following methods would you use to remove duplicate rows from a DataFrame `df` based on the values in the 'email' column, keeping the first occurrence?
- df.unique(subset=['email'])
- df.delete_duplicates(on='email')
- df.drop_duplicates(subset=['email'], keep='first') (Correct answer)
- df.remove(duplicates=True, column='email')
Correct answer: df.drop_duplicates(subset=['email'], keep='first')
The `drop_duplicates()` method is the correct function for removing duplicate rows. The `subset` parameter allows you to specify which columns to consider for identifying duplicates, and `keep='first'` ensures that the first observed instance of a duplicated row is retained.
Given a Pandas DataFrame named `sales`, which of the following code snippets correctly calculates the total sales for each 'Region' by summing the 'Sales' column?