โ† All Data Science with Python Certification Flashcard Decks

Data Science with Python Pandas DataFrame Operations Flashcards

6 cards from real Data Science with Python Certification practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 6 Data Science with Python Pandas DataFrame Operations flashcards as text
  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?

    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.

  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?

    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.

  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?

    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`.

  4. What is the primary purpose of the `df.loc[]` accessor in a Pandas DataFrame?

    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.

  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?

    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.

  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?

    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.

How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy

Data Science with Python Pandas DataFrame Operations Flashcards โ€” Data Science with Python Certification Study Cards with Answers