15 commonly asked Python interview questions
1. Explain the difference between a list and a tuple in Python.
istsare mutable, meaning their elements can be changed,whereastuplesare immutable,
L
making them more memory-efficient and faster. Lists use square brackets[], while tuples use
parentheses().
Example:
y_list = [1, 2, 3] # Mutable
m
my_tuple = (1, 2, 3) # Immutable
2. How do you handle missing data in a Pandas DataFrame?
● sedf.isnull().sum()to check missing values.
U
● Fill missing values usingdf.fillna(value, method='ffill'/'bfill').
● Remove missing values withdf.dropna().
Example:
f.fillna(df.mean()) # Fill NaNs with column mean
d
df.dropna() # Remove rows with NaNs
3. What is the purpose of thegroupby()function inPandas?
groupby()is
used to split a DataFrame into groupsbased on a column, apply functions, and
combine results.
Example:
df.groupby('Category')['Sales'].sum()
4. How can you merge two DataFrames in Pandas?
Usepd.merge()to combine DataFrames on a common column.
Example:
erged_df = pd.merge(df1, df2, on='ID', how='inner')
m # 'left', 'right',
'outer' also available
5. Explain the difference betweenlocandilocinPandas.
● : Label-based indexing.
loc[]
●
iloc[] : Integer-based positional indexing.
Example:
f.loc[2, 'Age'] # Access by row label
d
df.iloc[2, 1] # Access by row and column index
6. What are Python's built-in data types?
● umeric:int,float,complex
N
● Sequence:list,tuple,range,str
● Set:set,frozenset
● Mapping:dict
● Boolean:bool
● Binary:bytes,bytearray,memoryview
7. How do you read a CSV file into a Pandas DataFrame?
Usepd.read_csv().
Example:
df = pd.read_csv('file.csv')
8. What is the difference betweenapply()andmap()functions in Pandas?
● : Used for Series (element-wise transformations).
map()
●
apply() : Used for both Series and DataFrames (column-wiseor row-wise
transformations).
Example:
f['column'].map(lambda x: x * 2) # Applies function to each value
d
df.apply(lambda x: x.sum(), axis=0) # Sum of each column
9. How do you remove duplicates from a DataFrame?
Usedf.drop_duplicates().
Example:
df.drop_duplicates(subset=['column_name'], keep='first', inplace=True)
10. Explain the use of lambda functions in Python.
A lambda function is an anonymous, single-expression function.
Example:
quare = lambda x: x**2
s
print(square(4)) # Output: 16
11. What is the difference betweenjoin()andmerge()in Pandas?
● merge() : Used for complex joins (like SQL joins) oncolumns.
● : Used for joining on index.
join()
Example:
df1.join(df2, on='ID', how='left')
12. How can you concatenate two DataFrames vertically and horizontally?
Usepd.concat().
Example:
Vertical (stack rows)
#
df_vertical = pd.concat([df1, df2], axis=0)
Horizontal (add columns)
#
df_horizontal = pd.concat([df1, df2], axis=1)
13. What is the purpose of thepivot_table()functionin Pandas?
pivot_table()summarizes
data by aggregating valuesover a specified index and columns.
Example:
df.pivot_table(index='Category', values='Sales', aggfunc='sum')
14. How do you handle outliers in a dataset using Python?
● seIQR method: Remove values outside1.5 * IQR.
U
● UseZ-score: Remove values with|Z-score| > 3.
● UseWinsorization: Cap outliers instead of removingthem.
Example:
rom scipy import stats
f
df = df[(np.abs(stats.zscore(df['column'])) < 3)]
15. How do you filter rows in a Pandas DataFrame based on a condition?
Use boolean indexing.
Example:
filtered_df = df[df['Salary'] > 50000]