Loading

Quipoin Menu

Learn • Practice • Grow

python-for-ai / Series and DataFrames
interview

Q1. Scenario: You have a dictionary of student grades: {''Alice'':85, ''Bob'':92, ''Charlie'':78, ''Diana'':95}. Convert it to a pandas Series and compute the average grade.
import pandas as pd; grades = pd.Series({''Alice'':85, ''Bob'':92, ''Charlie'':78, ''Diana'':95}); mean = grades.mean() -> 87.5. Series is one-dimensional labeled array. Access by index: grades[''Alice'']=85. Great for key-value data.

Q2. Scenario: Create a DataFrame from a list of lists: data = [[''Alice'',25,85],[''Bob'',22,92],[''Charlie'',27,78]] with columns [''Name'',''Age'',''Score'']. Then display the first two rows.
df = pd.DataFrame(data, columns=[''Name'',''Age'',''Score'']); head = df.head(2). Output rows 0 and 1. DataFrame is 2D labeled structure. Also df.tail(2) for last rows. Essential for data exploration.

Q3. Scenario: Load a CSV file 'sales.csv' into a DataFrame. Then inspect its info, shape, and descriptive statistics.
df = pd.read_csv(''sales.csv''); df.info() gives data types and non-null counts; df.shape gives (rows, columns); df.describe() gives count, mean, std, min, quartiles, max for numeric columns. This is the first step in any data analysis.

Q4. Scenario: You have a DataFrame with column ''price''. Add a new column ''price_usd'' assuming 1 unit = 1.2 USD. Update the DataFrame.
df[''price_usd''] = df[''price''] * 1.2. This adds a new column. Alternatively, df.assign(price_usd = df[''price'']*1.2). Operations are vectorized. Use df.drop(''price_usd'', axis=1) to remove column.

Q5. Scenario: Create a DataFrame with missing values in column ''age''. Use fillna to replace missing with the median age.
df = pd.DataFrame({''age'': [25, None, 30, None, 35]}); median = df[''age''].median(); df[''age''].fillna(median, inplace=True). This imputes missing values. Also can use df.fillna(method=''ffill'') for forward fill.