60 Flashcards to Master Pandas, NumPy, and Matplotlib
Arrow keys or swipe to navigate cards
Boost your Python programming skills with 60 flashcards covering Pandas, NumPy, and Matplotlib. Learn essential concepts like data manipulation, numerical computations, and data visualization. Topics include working with DataFrames, array operations, broadcasting, creating plots, and customizing visualizations to master these key Python libraries.
What is a Pandas DataFrame? A Pandas DataFrame is a 2-dimensional, size-mutable, and tabular data structure with labeled axes (rows and columns).
How do you create a Pandas DataFrame from a dictionary?
Use the `pd.DataFrame()` function to create a DataFrame from a dictionary. Example:
data = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data)
print(df)
How do you check the first few rows of a DataFrame? Use the \(\texttt{DataFrame.head()}\) method to check the first few rows of a DataFrame.
How do you check the last few rows of a DataFrame? Use the \(\texttt{DataFrame.tail()}\) method to check the last few rows of a DataFrame.
How can you display the data types of all columns in a DataFrame? Use \(\texttt{DataFrame.dtypes}\) to display the data types of all columns in a DataFrame.
How do you read a CSV file into a Pandas DataFrame? Use \(\texttt{pd.read_csv('file.csv')}\) to read a CSV file into a Pandas DataFrame.
How can you write a Pandas DataFrame to a CSV file? Use \(\texttt{DataFrame.to_csv('file.csv')}\) to write a DataFrame to a CSV file.
How do you select a column in a DataFrame? Use \(\texttt{DataFrame['column_name']}\) or \(\texttt{DataFrame.column_name}\) to select a column.
How can you filter rows in a DataFrame where a column value equals 10?
Use the filtering syntax to select rows where a column value equals 10. Example:
data = {'column': [5, 10, 15]}
df = pd.DataFrame(data)
filtered_df = df[df['column'] == 10]
print(filtered_df)
What does the \(\texttt{groupby()}\) function do? The \(\texttt{groupby()}\) function groups data by specified columns and allows for aggregation or transformation on those groups.
How can you calculate the mean of a column in a DataFrame? Use \(\texttt{DataFrame['column'].mean()}\) to calculate the mean of a column.
How do you check for missing values in a DataFrame? Use \(\texttt{DataFrame.isnull()}\) to check for missing values in a DataFrame.
How do you drop rows with missing values from a DataFrame? Use \(\texttt{DataFrame.dropna()}\) to drop rows with missing values from a DataFrame.
How can you fill missing values in a DataFrame with a specific value? Use \(\texttt{DataFrame.fillna(value)}\) to fill missing values with a specific value.
How do you reset the index of a DataFrame? Use \(\texttt{DataFrame.reset_index()}\) to reset the index of a DataFrame.
How can you rename columns in a DataFrame?
Use the `rename` method to rename columns in a DataFrame. Example:
data = {'old_name': [1, 2, 3]}
df = pd.DataFrame(data)
df = df.rename(columns={'old_name': 'new_name'})
print(df)
How do you sort a DataFrame by a specific column?
Use the `sort_values` method to sort a DataFrame by a specific column. Example:
data = {'column_name': [3, 1, 2]}
df = pd.DataFrame(data)
sorted_df = df.sort_values('column_name')
print(sorted_df)
How do you concatenate two DataFrames vertically? Use \(\texttt{pd.concat([df1, df2])}\) to concatenate two DataFrames vertically.
How do you merge two DataFrames on a specific column?
Use the `pd.merge` function to merge two DataFrames on a specific column. Example:
df1 = pd.DataFrame({'column_name': [1, 2], 'value1': ['A', 'B']})
df2 = pd.DataFrame({'column_name': [1, 2], 'value2': ['X', 'Y']})
merged_df = pd.merge(df1, df2, on='column_name')
print(merged_df)
What is the purpose of \(\texttt{DataFrame.describe()}\)? \(\texttt{DataFrame.describe()}\) generates summary statistics of numerical columns in the DataFrame.
How do you get the unique values in a column? Use \(\texttt{DataFrame['column'].unique()}\) to get the unique values in a column.
How can you apply a function to every element in a column?
Use the `apply` method to apply a function to every element in a column. Example:
data = {'column': [1, 2, 3]}
df = pd.DataFrame(data)
df['squared'] = df['column'].apply(lambda x: x**2)
print(df)
How do you create a new column in a DataFrame?
Assign values to a new column in the DataFrame. Example:
data = {'column': [1, 2, 3]}
df = pd.DataFrame(data)
df['new_column'] = df['column'] * 2
print(df)
What does \(\texttt{pivot_table()}\) do in Pandas? \(\texttt{pivot_table()}\) creates a spreadsheet-style pivot table for summarizing data in a DataFrame.
How do you change the data type of a column in a DataFrame? Use \(\texttt{DataFrame['column'].astype(new_type)}\) to change the data type of a column in a DataFrame.
What is NumPy primarily used for? NumPy is primarily used for numerical computations and working with multi-dimensional arrays in Python.
How do you create a NumPy array from a Python list? Use \(\texttt{np.array([1, 2, 3])}\) to create a NumPy array from a Python list.
How do you create an array of zeros in NumPy? Use \(\texttt{np.zeros((rows, cols))}\) to create an array of zeros with the specified shape.
How do you create an array of ones in NumPy? Use \(\texttt{np.ones((rows, cols))}\) to create an array of ones with the specified shape.
How can you create a range of numbers in NumPy? Use \(\texttt{np.arange(start, stop, step)}\) to create a range of numbers with a specified step size.
What is the difference between \(\texttt{np.linspace()}\) and \(\texttt{np.arange()}\)? \(\texttt{np.linspace(start, stop, num)}\) creates evenly spaced numbers, while \(\texttt{np.arange()}\) creates numbers with a fixed step size.
How do you reshape a NumPy array? Use \(\texttt{array.reshape(new_shape)}\) to change the shape of a NumPy array.
What does the \(\texttt{ndim}\) attribute of a NumPy array return? The \(\texttt{ndim}\) attribute returns the number of dimensions of the array.
How do you find the shape of a NumPy array? Use the \(\texttt{shape}\) attribute to find the shape of a NumPy array.
How can you perform element-wise addition of two NumPy arrays? Use the \(\texttt{+}\) operator, such as \(\texttt{array1 + array2}\), to perform element-wise addition.
How do you calculate the mean of all elements in a NumPy array? Use \(\texttt{np.mean(array)}\) to calculate the mean of all elements in a NumPy array.
How do you find the maximum value in a NumPy array? Use \(\texttt{np.max(array)}\) to find the maximum value in a NumPy array.
How do you find the index of the maximum value in a NumPy array? Use \(\texttt{np.argmax(array)}\) to find the index of the maximum value in a NumPy array.
How can you stack two NumPy arrays vertically? Use \(\texttt{np.vstack([array1, array2])}\) to stack two arrays vertically.
How can you stack two NumPy arrays horizontally? Use \(\texttt{np.hstack([array1, array2])}\) to stack two arrays horizontally.
What is broadcasting in NumPy? Broadcasting is a technique that allows NumPy to perform operations on arrays of different shapes by expanding them to a compatible shape.
How do you generate a 2x2 identity matrix in NumPy? Use \(\texttt{np.eye(2)}\) to generate a 2x2 identity matrix in NumPy.
How can you filter elements in a NumPy array based on a condition? Use a conditional expression, such as \(\texttt{array[array > 5]}\), to filter elements in a NumPy array.
What does the \(\texttt{np.dot()}\) function do? The \(\texttt{np.dot()}\) function performs the dot product of two arrays or matrices.
How do you find the transpose of a NumPy array? Use \(\texttt{array.T}\) or \(\texttt{np.transpose(array)}\) to find the transpose of a NumPy array.
What is Matplotlib used for? Matplotlib is a library for creating static, interactive, and animated visualizations in Python.
How do you create a basic line plot in Matplotlib? Use \(\texttt{plt.plot(x, y)}\) to create a basic line plot, and \(\texttt{plt.show()}\) to display it.
How can you add a title to a plot? Use \(\texttt{plt.title('Your Title')}\) to add a title to a plot.
How can you label the x-axis and y-axis in a plot? Use \(\texttt{plt.xlabel('X Label')}\) and \(\texttt{plt.ylabel('Y Label')}\) to label the x-axis and y-axis, respectively.
How do you create a scatter plot in Matplotlib? Use \(\texttt{plt.scatter(x, y)}\) to create a scatter plot.
How can you customize the color and style of a line in a plot? Use parameters like \(\texttt{color='red'}\) and \(\texttt{linestyle='--'}\) in \(\texttt{plt.plot()}\).
How do you create a bar chart in Matplotlib? Use \(\texttt{plt.bar(x, height)}\) to create a bar chart.
How can you create a histogram in Matplotlib? Use \(\texttt{plt.hist(data, bins)}\) to create a histogram.
What is the purpose of \(\texttt{plt.legend()}\)? \(\texttt{plt.legend()}\) adds a legend to the plot to label different data series.
How do you create subplots in Matplotlib? Use \(\texttt{plt.subplot(rows, cols, index)}\) to create and select subplots in a grid layout.
How can you adjust the size of a plot in Matplotlib? Use \(\texttt{plt.figure(figsize=(width, height))}\) to adjust the size of a plot.
How do you save a plot to a file in Matplotlib? Use \(\texttt{plt.savefig('filename.png')}\) to save a plot to a file.
How do you set limits for the x-axis and y-axis? Use \(\texttt{plt.xlim(min, max)}\) and \(\texttt{plt.ylim(min, max)}\) to set axis limits.
How do you display a grid on a plot? Use \(\texttt{plt.grid(True)}\) to display a grid on a plot.
How can you change the style of a plot in Matplotlib? Use \(\texttt{plt.style.use('style_name')}\), where \(\texttt{style_name}\) is a predefined style like 'ggplot' or 'seaborn'.