Intro to NumPy/pandas (concept)

Computer Science · Python

Question 1 of 10

0

    Which pandas function combines two DataFrames by matching rows on a shared key column?

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Intro to NumPy/pandas (concept) explained

    NumPy and pandas are third-party libraries for working with numbers and tables in Python. NumPy provides arrays that hold many values of the same type and can be operated on all at once. pandas builds on NumPy with DataFrames, which are tables with labelled columns for data analysis.

    Neither library comes with Python, so install them with pip install numpy pandas. A NumPy array is created with np.array, and arithmetic applies to every element without an explicit loop, which is much faster for large data. A pandas DataFrame works like a spreadsheet in code: you can select a column by name, then filter, group or summarise it. The usual import names are np and pd. The details of these libraries change between versions, so check the documentation for the version you have installed.

    import numpy as np
    import pandas as pd
    
    prices = np.array([4.0, 2.5, 3.0])
    print(prices * 2)
    
    df = pd.DataFrame({"item": ["tea", "milk"], "cost": [4.0, 2.5]})
    print(df["cost"].sum())
    

    Worked example

    Trace the code above.

    1. prices * 2 multiplies every element, giving 8.0, 5.0 and 6.0.
    2. df["cost"] selects the cost column, and .sum() adds its values: 4.0+2.5=6.54.0 + 2.5 = 6.5.
    3. The second print therefore displays 6.5.

    What you will practise

    105 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.

    Example questions

    • NumPy provides:
    • Pandas provides:
    • A DataFrame is:
    • NumPy arrays are faster than lists because:
    • Pandas can:
    • Which statement imports NumPy with the conventional alias np?
    • The conventional alias for the pandas library is:
    • What does the .shape attribute of a NumPy array return?

    …and 97 more.