Python Libraries: Numpy and Pandas
37 Questions
1 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the primary purpose of a software library in Python?

  • To reinvent the wheel
  • To provide a collection of pre-written code (correct)
  • To replace the Python interpreter
  • To create a new programming language
  • What are the two ways to import a Python library?

  • Importing the whole library with an alias or importing a specific object
  • Importing the whole library with an alias or importing a specific function (correct)
  • Importing the whole library or importing a specific object
  • Importing the whole library or importing a specific function
  • What is the primary use of the NumPy library in Python?

  • For model deployment
  • For data mining and data scraping
  • For creating and manipulating N-dimensional arrays (correct)
  • For data visualization
  • What is the purpose of the np.reshape function in NumPy?

    <p>To transform a 1D array into a multidimensional array</p> Signup and view all the answers

    What is the dimension of a 2D NumPy array?

    <p>Array of Arrays</p> Signup and view all the answers

    What will happen if you try to reshape a 12-element 1D array into a 4x4 2D array?

    <p>It will generate an error</p> Signup and view all the answers

    What is the method to display the first and last 5 rows of a DataFrame?

    <p>myDataFrame.head()</p> Signup and view all the answers

    How can you create a DataFrame from a csv file?

    <p>myDataFrame = pd.read_csv('myfile.csv')</p> Signup and view all the answers

    What is the attribute of an Iris flower characterized by the length in centimeters?

    <p>sepal_length</p> Signup and view all the answers

    How can you save a DataFrame to a csv file?

    <p>myDataFrame.to_csv('myOutputFile.csv')</p> Signup and view all the answers

    How many Iris species are in the Iris sample data?

    <p>3</p> Signup and view all the answers

    What is the main library used for dealing with numeric and string data?

    <p>Pandas</p> Signup and view all the answers

    What is the purpose of the 'describe' function in Pandas Series?

    <p>To provide a full numerical summary of the series</p> Signup and view all the answers

    What is the default name of the first column in a Pandas Series?

    <p>Index</p> Signup and view all the answers

    How can you access a value in a Pandas Series using a custom index?

    <p>Using square brackets with the custom index value</p> Signup and view all the answers

    What is the purpose of the 'read_csv' function in Pandas?

    <p>To read data from a CSV file into a DataFrame</p> Signup and view all the answers

    What is a Pandas DataFrame?

    <p>A 2D collection of data with custom indices and headers</p> Signup and view all the answers

    What is the purpose of the np.transpose function in NumPy?

    <p>To replace rows with columns in a 2D array</p> Signup and view all the answers

    What is the output of np.sort(arr_2D,axis=1)?

    <p>Row-wise sorted array</p> Signup and view all the answers

    What does np.sum(grades,axis=1) calculate?

    <p>The sum of each row in the array</p> Signup and view all the answers

    What does grades[0,0] access in the array?

    <p>The element at the first row and first column</p> Signup and view all the answers

    What does grades[0:2] access in the array?

    <p>The first and second rows</p> Signup and view all the answers

    What does grades[:,0] access in the array?

    <p>The entire first column</p> Signup and view all the answers

    What does grades[:,-1] access in the array?

    <p>The entire last column</p> Signup and view all the answers

    What is the purpose of the reshape function in NumPy?

    <p>To convert a 1D array to a 2D array</p> Signup and view all the answers

    What is the output of np.min(grades,axis=1)?

    <p>The minimum of each row</p> Signup and view all the answers

    What is the main difference between NumPy arrays and Pandas Series/DataFrames?

    <p>NumPy arrays are optimized for homogenous numeric data, while Pandas supports heterogeneous data</p> Signup and view all the answers

    In DataFrames, which statistics are calculated by column?

    <p>Only for numeric columns</p> Signup and view all the answers

    What is the purpose of the describe() function?

    <p>To calculate statistics for numeric columns</p> Signup and view all the answers

    How can you calculate the mean of a DataFrame?

    <p>Using the <code>mean()</code> function</p> Signup and view all the answers

    What is the output of data.to_numpy()?

    <p>A NumPy array</p> Signup and view all the answers

    How can you convert a NumPy array into a DataFrame?

    <p>Using the <code>pd.DataFrame()</code> constructor</p> Signup and view all the answers

    What is the purpose of the iloc[] function?

    <p>To select rows and columns in a DataFrame</p> Signup and view all the answers

    What is the output of data.iloc[:, 0:4].to_numpy()?

    <p>A NumPy array with the first 4 columns</p> Signup and view all the answers

    What is the purpose of the head() function?

    <p>To display the first few rows of a DataFrame</p> Signup and view all the answers

    How can you add columns to a DataFrame?

    <p>Using the <code>columns=[]</code> argument</p> Signup and view all the answers

    What is the purpose of the std() function?

    <p>To calculate the standard deviation of a DataFrame</p> Signup and view all the answers

    Study Notes

    NumPy Array

    • Importing NumPy: import numpy as np
    • Creating a 1D array: arr = np.array([1,2,3,4,5,6,7,8,9,10,11,12])
    • Reshaping a 1D array into a 2D array: arr_2D = arr.reshape(4,3)

    Transposing NumPy Arrays

    • Transposing a 2D array: arr_2D_transposed = np.transpose(arr_2D)

    NumPy Sorting

    • Sorting a 2D array: np.sort(arr_2D, axis=None) (sorts the whole array)
    • Row-wise sorting: np.sort(arr_2D, axis=1)
    • Column-wise sorting: np.sort(arr_2D, axis=0)

    NumPy Calculation Functions

    • Summation:
      • Row-wise: grades.sum(axis=1)
      • Column-wise: grades.sum(axis=0)
      • All grades: grades.sum(axis=None)
    • Minimum:
      • Row-wise: grades.min(axis=1)
      • Column-wise: grades.min(axis=0)
      • All grades: grades.min(axis=None)

    Indexing and Slicing

    • Accessing individual elements: grades[row index, col index]
    • Selecting one row: grades[row index]
    • Selecting multiple rows: grades[row index from : row index to]
    • Selecting a subset of columns: grades[:,0] (selects all rows, column 0)
    • Selecting a subset of columns: grades[:, 0:2] (selects all rows, columns 0, 1)
    • Negative indices: grades[:, -1] (accesses the last column)

    Pandas: Series and DataFrames

    • Creating a Series: grades = pd.Series([87, 100, 94])
    • Creating a DataFrame: grades = pd.DataFrame([87, 100, 94], index=['First', 'Second', 'final'])
    • Accessing Series using string indices: grades['First']

    DataFrames

    • Creating a DataFrame from a file: myDataFrame = pd.read_csv("myfile.csv")
    • Displaying the first and last 5 rows: myDataFrame.head() and myDataFrame.tail()
    • Statistics:
      • Mean, min, max, std, var: data.describe()
      • Count, mean, min, max, std: data.mean(), data.min(), data.max(), data.std()

    DataFrames and NumPy

    • Converting a DataFrame to a NumPy array: numpy_from_dataFrame = data.to_numpy()
    • Converting a NumPy array to a DataFrame: dataFrame_from_numpy = pd.DataFrame(numpy_from_dataFrame, columns = […])

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Learn about popular Python libraries, including Numpy and Pandas, and how they can be used to improve your programming experience. This quiz covers the basics of Python libraries and their applications.

    More Like This

    Use Quizgecko on...
    Browser
    Browser