Title: Python For Data Science — LearnCoachAssist

Q: What function creates a NumPy array from a Python list?
A: np.array([1, 2, 3]) converts a Python list into a NumPy ndarray.

Q: How do you create a 1D array of zeros with 5 elements in NumPy?
A: np.zeros(5) returns array([0., 0., 0., 0., 0.]).

Q: How do you create a 3x3 identity matrix in NumPy?
A: np.eye(3) returns a 3×3 array with ones on the diagonal and zeros elsewhere.

Q: What does np.arange(0, 10, 2) return?
A: It returns array([0, 2, 4, 6, 8]) — values from 0 up to (not including) 10 with step 2.

Q: How do you generate 5 evenly spaced values between 0 and 1 in NumPy?
A: np.linspace(0, 1, 5) returns array([0., 0.25, 0.5, 0.75, 1.]).

Q: How do you access the element at row 1, column 2 of a 2D NumPy array arr?
A: arr[1, 2] uses zero-based indexing to retrieve the element at the second row, third column.

Q: What does arr[:, 0] select from a 2D NumPy array?
A: It selects all rows of the first column, returning a 1D array.

Q: How do you slice rows 1 through 3 (inclusive) from a NumPy array?
A: arr[1:4] selects rows at indices 1, 2, and 3 (the stop index is exclusive).

Q: What is broadcasting in NumPy?
A: Broadcasting is NumPy's ability to perform element-wise operations on arrays of different shapes by automatically expanding the smaller array to match dimensions.

Q: Give an example of NumPy broadcasting with a scalar.
A: arr * 3 multiplies every element of arr by 3 without an explicit loop — the scalar is broadcast across the array.

Q: How do you reshape a 1D array of 12 elements into a 3x4 matrix?
A: arr.reshape(3, 4) returns a 3×4 view of the array (total elements must match).

Q: What does arr.ravel() do?
A: It flattens a multi-dimensional array into a 1D array, returning a view when possible.

Q: What is a universal function (ufunc) in NumPy?
A: A ufunc is a function that operates element-wise on ndarrays, e.g., np.sqrt(arr), np.exp(arr).

Q: How do you compute the element-wise square root of a NumPy array?
A: np.sqrt(arr) applies the square root to each element and returns a new array.

Q: What does np.where(arr > 0, arr, 0) do?
A: It returns an array where positive values are kept and negative/zero values are replaced with 0.

Q: How do you create a Pandas DataFrame from a dictionary?
A: pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]}) creates a DataFrame with two columns.

Q: What is the difference between df.loc[] and df.iloc[]?
A: loc selects by label (index/column names), while iloc selects by integer position.

Q: How do you select rows where column 'age' is greater than 30?
A: df[df['age'] > 30] returns a filtered DataFrame using boolean indexing.

Q: How do you select specific rows and columns by label in Pandas?
A: df.loc[0:3, ['name', 'age']] selects rows 0–3 and only the 'name' and 'age' columns.

Q: How do you select the first 5 rows and first 2 columns by position?
A: df.iloc[:5, :2] uses integer-based indexing to slice rows and columns.

Q: How do you sort a DataFrame by column 'price' in descending order?
A: df.sort_values('price', ascending=False) returns the DataFrame sorted from highest to lowest price.

Q: How do you sort a DataFrame by multiple columns?
A: df.sort_values(['col1', 'col2'], ascending=[True, False]) sorts by col1 ascending, then col2 descending.

Q: How do you reset the index of a DataFrame?
A: df.reset_index(drop=True) resets the index to default integers; drop=True discards the old index.

Q: How do you detect duplicate rows in a Pandas DataFrame?
A: df.duplicated() returns a boolean Series that is True for duplicate rows.

Q: How do you remove duplicate rows from a DataFrame?
A: df.drop_duplicates() removes duplicate rows, keeping the first occurrence by default.

Q: How do you convert a column to lowercase strings in Pandas?
A: df['col'].str.lower() uses the .str accessor to apply the lower() string method.

Q: How do you strip whitespace from string column values?
A: df['col'].str.strip() removes leading and trailing whitespace from each value.

Q: How do you convert a column's data type to integer in Pandas?
A: df['col'] = df['col'].astype(int) casts the column to integer type.

Q: How do you convert a string column to datetime in Pandas?
A: pd.to_datetime(df['date_col']) parses strings into datetime64 objects.

Q: How do you replace specific values in a DataFrame column?
A: df['col'].replace({'old_val': 'new_val'}) substitutes matching values in the column.

Q: How do you create a line plot with matplotlib?
A: plt.plot(x, y) followed by plt.show() draws a basic line plot.

Q: How do you add a title and axis labels to a matplotlib plot?
A: Use plt.title('Title'), plt.xlabel('X'), and plt.ylabel('Y') before calling plt.show().

Q: How do you create a bar chart in matplotlib?
A: plt.bar(categories, values) creates a vertical bar chart for the given categories and values.

Q: How do you create a scatter plot with matplotlib?
A: plt.scatter(x, y) plots individual data points as dots on the chart.

Q: How do you create a histogram in matplotlib?
A: plt.hist(data, bins=20) displays the frequency distribution of data using 20 bins.

Q: How do you create a heatmap with seaborn?
A: sns.heatmap(df.corr(), annot=True, cmap='coolwarm') displays a color-coded correlation matrix.

Q: How do you create a seaborn countplot?
A: sns.countplot(x='category', data=df) shows the count of observations in each category as bars.

Q: How do you save a matplotlib figure to a file?
A: plt.savefig('plot.png', dpi=300, bbox_inches='tight') saves the current figure as a PNG file.

Q: How do you create subplots in matplotlib?
A: fig, axes = plt.subplots(1, 2, figsize=(10, 4)) creates a figure with 1 row and 2 columns of axes.

Q: What does the seaborn pairplot function do?
A: sns.pairplot(df) creates a grid of scatter plots for every pair of numeric columns, with histograms on the diagonal.

Q: What does the %timeit magic command do in Jupyter?
A: It runs a statement multiple times and reports the average execution time, useful for benchmarking code.

Q: What does %matplotlib inline do in a Jupyter notebook?
A: It configures matplotlib to display plots inline directly below the code cell.

Q: What Jupyter shortcut runs the current cell and moves to the next?
A: Shift + Enter executes the current cell and advances to the next cell.

Q: How do you add a new cell above the current cell in Jupyter?
A: Press A in command mode (press Esc first) to insert a cell above.

Q: What does the %%time cell magic do in Jupyter?
A: It measures the wall time and CPU time for the entire cell's execution (single run, unlike %timeit).

Q: What are the main data types for numerical data in Pandas?
A: int64 for integers and float64 for floating-point numbers; use df.dtypes to inspect.

Q: How do you create a categorical column in Pandas?
A: df['col'] = df['col'].astype('category') converts the column to the memory-efficient category dtype.

Q: What is the advantage of the categorical data type in Pandas?
A: It reduces memory usage and speeds up operations like groupby on columns with a limited number of unique values.

Q: How do you extract the year from a datetime column?
A: df['date_col'].dt.year uses the .dt accessor to extract the year component.

Q: How do you extract the day of the week from a datetime column?
A: df['date_col'].dt.dayofweek returns integers 0 (Monday) through 6 (Sunday).

Q: How do you check for missing values in a DataFrame?
A: df.isnull().sum() returns the count of NaN values per column.

Q: What does df.dropna() do?
A: It removes rows (by default) that contain any NaN values from the DataFrame.

Q: How do you drop rows only if all values are NaN?
A: df.dropna(how='all') removes rows where every column value is NaN.

Q: How do you fill missing values with the column mean?
A: df['col'].fillna(df['col'].mean()) replaces NaN values with the column's mean.

Q: How do you fill missing values with the previous row's value?
A: df['col'].fillna(method='ffill') performs forward fill, propagating the last valid value.

Q: How do you interpolate missing values linearly in Pandas?
A: df['col'].interpolate(method='linear') fills NaN values using linear interpolation between existing data points.

Q: What does df.isna().any() return?
A: A boolean Series indicating which columns contain at least one missing value.

Q: What does pd.isnull(value) check?
A: It returns True if value is NaN, None, or NaT (Not a Time).

Q: How do you group a DataFrame by a column and compute the mean?
A: df.groupby('category')['value'].mean() computes the mean of 'value' for each unique category.

Q: How do you apply multiple aggregations to a groupby object?
A: df.groupby('col').agg(['mean', 'sum', 'count']) returns a DataFrame with multiple summary statistics.

Q: What is the difference between transform and agg in groupby?
A: agg returns a reduced result (one row per group), while transform returns a result with the same shape as the input.

Q: How do you use transform to normalize values within groups?
A: df.groupby('group')['val'].transform(lambda x: (x - x.mean()) / x.std()) standardizes values within each group.

Q: How do you apply a custom function to each group?
A: df.groupby('col').apply(my_function) applies my_function to each group DataFrame.

Q: How do you count the size of each group?
A: df.groupby('col').size() returns a Series with the number of rows in each group.

Q: What does pd.merge(df1, df2, on='key') do by default?
A: It performs an inner join, keeping only rows where the key exists in both DataFrames.

Q: How do you perform a left join in Pandas?
A: pd.merge(df1, df2, on='key', how='left') keeps all rows from df1 and matches from df2.

Q: How do you perform a right join in Pandas?
A: pd.merge(df1, df2, on='key', how='right') keeps all rows from df2 and matches from df1.

Q: How do you perform an outer join in Pandas?
A: pd.merge(df1, df2, on='key', how='outer') keeps all rows from both DataFrames, filling NaN where keys don't match.

Q: How do you merge DataFrames on columns with different names?
A: pd.merge(df1, df2, left_on='id', right_on='user_id') joins using differently named key columns.

Q: What does pd.concat([df1, df2]) do?
A: It stacks DataFrames vertically (along axis 0) by default, appending rows.

Q: How do you concatenate DataFrames side by side?
A: pd.concat([df1, df2], axis=1) joins DataFrames horizontally, aligning on the index.

Q: How do you join DataFrames on their index?
A: df1.join(df2, how='inner') performs an index-based join between two DataFrames.

Q: What is one-hot encoding and how do you do it in Pandas?
A: One-hot encoding converts categorical variables into binary columns. Use pd.get_dummies(df, columns=['col']).

Q: How do you bin continuous data into discrete intervals?
A: pd.cut(df['age'], bins=[0, 18, 35, 60, 100], labels=['child', 'young', 'middle', 'senior']) creates categorical bins.

Q: How do you bin data into equal-frequency bins?
A: pd.qcut(df['value'], q=4, labels=['Q1', 'Q2', 'Q3', 'Q4']) creates quartile-based bins with roughly equal counts.

Q: What is feature scaling and why is it important?
A: Feature scaling normalizes the range of features so that algorithms sensitive to magnitude (e.g., KNN, SVM) perform correctly. Common methods: min-max and standardization.

Q: How do you apply min-max scaling using scikit-learn?
A: from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
df[['col']] = scaler.fit_transform(df[['col']])

Q: How do you standardize features to have mean 0 and std 1?
A: from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
df[['col']] = scaler.fit_transform(df[['col']])

Q: How do you create a new column based on existing columns?
A: df['total'] = df['price'] * df['quantity'] creates a new column using vectorized arithmetic.

Q: How do you apply a custom function element-wise to a column?
A: df['col'].apply(lambda x: x ** 2) applies the lambda function to each element of the column.

Q: What does df.describe() return?
A: It returns summary statistics (count, mean, std, min, 25%, 50%, 75%, max) for all numeric columns.

Q: What does df.info() display?
A: It prints the DataFrame's column names, non-null counts, data types, and memory usage — useful for a quick data overview.

Q: How do you count unique values in a column?
A: df['col'].value_counts() returns a Series with unique values as the index and their counts sorted descending.

Q: How do you compute the correlation matrix of a DataFrame?
A: df.corr() returns a DataFrame of pairwise Pearson correlation coefficients for all numeric columns.

Q: What does df.shape return?
A: A tuple (rows, columns) indicating the dimensions of the DataFrame.

Q: How do you view the first 10 rows of a DataFrame?
A: df.head(10) returns the first 10 rows (default is 5 if no argument is given).

Q: How do you get the unique values of a column?
A: df['col'].unique() returns an array of all distinct values in the column.

Q: How do you count the number of unique values in a column?
A: df['col'].nunique() returns the count of distinct values, excluding NaN by default.

Q: How do you rename columns in a Pandas DataFrame?
A: df.rename(columns={'old_name': 'new_name'}) returns a DataFrame with the specified columns renamed.

Q: How do you set a column as the DataFrame index?
A: df.set_index('col') makes the specified column the new row index.

Q: How do you create a NumPy array of random integers?
A: np.random.randint(0, 100, size=(3, 4)) creates a 3×4 array of random integers between 0 and 99.

Q: How do you compute the dot product of two NumPy arrays?
A: np.dot(a, b) or a @ b computes the matrix/dot product of arrays a and b.

Q: What does arr.T do for a NumPy array?
A: It returns the transpose of the array, swapping rows and columns.

Q: How do you stack two NumPy arrays vertically?
A: np.vstack([a, b]) stacks arrays on top of each other along axis 0.

Q: How do you perform element-wise comparison in NumPy?
A: arr > 5 returns a boolean array where each element indicates whether the condition is True or False.

Q: What does df.pivot_table(values='sales', index='region', columns='quarter', aggfunc='sum') do?
A: It creates a pivot table summarizing total sales by region (rows) and quarter (columns).

Q: How do you use df.melt()?
A: df.melt(id_vars=['id'], value_vars=['col1', 'col2']) unpivots columns into rows, converting wide format to long format.

Q: How do you read a CSV file into a Pandas DataFrame?
A: pd.read_csv('file.csv') reads the file and returns a DataFrame; use sep='\t' for TSV files.

Q: How do you write a DataFrame to a CSV file?
A: df.to_csv('output.csv', index=False) saves the DataFrame without the row index.

Q: How do you select rows using multiple conditions in Pandas?
A: df[(df['age'] > 25) & (df['city'] == 'NYC')] — wrap each condition in parentheses and use & (and) or | (or).