Pandas Interview Questions and Answers
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. What is the difference between loc and iloc in Pandas?
loc is label-based indexing, while iloc is integer-based indexing.
Ques 2. How to drop a column in a DataFrame?
You can drop a column using the drop() method: df.drop('ColumnName', axis=1, inplace=True)
Ques 3. Explain the use of groupby() in Pandas.
groupby() is used to group DataFrame by a column and perform aggregate functions.
Example:
df.groupby('Column').mean()
Ques 4. What is the purpose of the apply() function in Pandas?
apply() is used to apply a function along the axis of a DataFrame.
Example:
df['Column'].apply(lambda x: x*2)
Ques 5. How to filter rows in a DataFrame based on a condition?
You can use boolean indexing to filter rows based on a condition: df[df['Column'] > 10]
Ques 6. What is the purpose of the pivot_table() function?
pivot_table() is used to create a spreadsheet-style pivot table as a DataFrame.
Example:
pd.pivot_table(df, values='Value', index='Index', columns='Column', aggfunc=np.sum)
Ques 7. How to handle duplicate values in a DataFrame?
You can use drop_duplicates() to remove duplicate rows: df.drop_duplicates()
Ques 8. Explain the purpose of the iterrows() function in Pandas.
iterrows() is used to iterate over DataFrame rows as (index, Series) pairs.
Example:
for index, row in df.iterrows(): print(index, row['Column'])
Ques 9. Explain the use of melt() function in Pandas.
melt() is used to reshape or transform data by unpivoting it.
Example:
pd.melt(df, id_vars=['ID'], value_vars=['Var1', 'Var2'])
Ques 10. What is the purpose of the to_csv() method in Pandas?
to_csv() is used to write a DataFrame to a CSV file.
Example:
df.to_csv('output.csv', index=False)
Ques 11. How to calculate correlation between columns in a DataFrame?
You can use the corr() method: df.corr()
Ques 12. Explain the purpose of the get_dummies() function in Pandas.
get_dummies() is used for one-hot encoding categorical variables.
Example:
pd.get_dummies(df['Category'])
Most helpful rated by users:
Related interview subjects
SciPy interview questions and answers - Total 30 questions |
Generative AI interview questions and answers - Total 30 questions |
NumPy interview questions and answers - Total 30 questions |
Python interview questions and answers - Total 106 questions |
Python Pandas interview questions and answers - Total 48 questions |
Python Matplotlib interview questions and answers - Total 30 questions |
Django interview questions and answers - Total 50 questions |
Pandas interview questions and answers - Total 30 questions |
Deep Learning interview questions and answers - Total 29 questions |
Flask interview questions and answers - Total 40 questions |
PySpark interview questions and answers - Total 30 questions |
PyTorch interview questions and answers - Total 25 questions |
Data Science interview questions and answers - Total 23 questions |