Python Pandas Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is Pandas in Python?
Pandas is an open-source data manipulation and analysis library for Python.
Ques 2. How do you import the Pandas library?
import pandas as pd
Ques 3. How do you create a DataFrame in Pandas?
pd.DataFrame(data)
Example:
df = pd.DataFrame({'column1': [1, 2, 3], 'column2': ['a', 'b', 'c']})
Ques 4. How do you select specific columns from a DataFrame?
df[['column1', 'column2']]
Ques 5. How can you apply a function to each element in a DataFrame?
Use the apply function. df.apply(my_function)
Ques 6. How can you rename columns in a Pandas DataFrame?
Use the rename function. df.rename(columns={'old_name': 'new_name'})
Ques 7. Explain the difference between Series and DataFrame in Pandas.
A Series is a one-dimensional labeled array, and a DataFrame is a two-dimensional table.
Ques 8. How do you convert a Pandas DataFrame to a NumPy array?
Use the values attribute. df.values
Ques 9. How can you reset the index of a Pandas DataFrame?
Use the reset_index function. df.reset_index()
Ques 10. How do you sort a Pandas DataFrame by a specific column?
Use the sort_values function. df.sort_values(by='column')
Ques 11. What is the purpose of the to_csv function in Pandas?
to_csv is used to write a DataFrame to a CSV file.
Example:
df.to_csv('output.csv', index=False)
Ques 12. How do you check for the existence of a specific value in a Pandas DataFrame?
Use the isin function. df['column'].isin([value])
Ques 13. What is the purpose of the read_csv function in Pandas?
read_csv is used to read data from a CSV file into a DataFrame.
Example:
df = pd.read_csv('file.csv')
Ques 14. Explain the use of the describe function in Pandas.
describe generates descriptive statistics of a DataFrame, excluding NaN values.
Example:
df.describe()
Ques 15. How can you drop columns from a Pandas DataFrame?
Use the drop function. df.drop(['column1', 'column2'], axis=1)
Ques 16. How do you handle duplicate values in a Pandas DataFrame?
Use the drop_duplicates() function. df.drop_duplicates()
Ques 17. Explain the purpose of the to_datetime() function in Pandas.
to_datetime() is used to convert the argument to datetime.
Example:
df['date_column'] = pd.to_datetime(df['date_column'])
Ques 18. How do you change the data type of a Pandas Series or DataFrame column?
Use the astype() function. df['column'] = df['column'].astype('new_dtype')
Ques 19. Explain the purpose of the nlargest() function in Pandas.
nlargest() returns the first n largest elements from a DataFrame or Series.
Example:
df.nlargest(5, 'column')
Ques 20. How can you create a Pandas DataFrame from a dictionary of Series or dictionaries?
Use the pd.DataFrame() constructor. df = pd.DataFrame({'column1': series1, 'column2': series2})
Ques 21. What is the purpose of the to_excel() function in Pandas?
to_excel() is used to write a DataFrame to an Excel file.
Example:
df.to_excel('output.xlsx', index=False)
Ques 22. How do you calculate the correlation matrix for a Pandas DataFrame?
Use the corr() function. df.corr()
Most helpful rated by users:
Related interview subjects
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 |
PySpark interview questions and answers - Total 30 questions |
Flask interview questions and answers - Total 40 questions |
PyTorch interview questions and answers - Total 25 questions |
Data Science interview questions and answers - Total 23 questions |
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 |