Python Interview Prep: Questionnaire Guide Part 3

Pallavi Padav
Women in Technology
4 min readJun 25, 2024

Welcome to the interview preparation guide series part 3. I have covered some of the Python coding FAQ in part 1 and part 2, here is the collection of dataframe related frequently asked Python coding interview questions. These questions will not just ace your interview but come in handy once you start working on real-time data sets.

Please solve these questionnaires independently and then compare the logic mentioned below. Please share any other solutions to these questions in the comment section.

Q1. Transform the categorical value true/false to 1/0 in a data frame without using one-hot encoding or get_dummies.

df = pd.DataFrame({'A': [True, False, True, False]})
df

Solution1:

df['A'] = df['A'].astype(int)
df

Solution 2:

df = pd.DataFrame({'A': [True, False, True, False]})
df['A'] = df['A'].map({True: 1, False: 0})

Solution 3:

df = pd.DataFrame({'A': [True, False, True, False]})
df['A'].replace([True, False],[1, 0], inplace=True)
df

Q2. Write a code to Sort a dataframe by two columns.

import pandas as pd
data = [['Tom', 10, 'UK'], ['Nick', 15, 'USA'], ['Juli', 14, 'Aus'], ['Atul', 10, 'IND'],]
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Citizen'])
df.head()
df.sort_values(by = ['Name', 'Age'], inplace = True)
df

Q3. Find the row which has the maximum value of a given column.

df['Age'].idxmax()

output : 1

df.iloc[df['Age'].idxmax()]

Q4. How can you split a column that contains strings into multiple columns?

data = [['Tom class2', 10, 'UK'], ['Nick class5', 15, 'USA'], ['Juli class4', 14, 'Aus'], ['Atul class2', 10, 'IND'],]
df = pd.DataFrame(data, columns = ['Name', 'Age','Citizen'])
df_col = df['Name'].str.split(pat=" ", expand=True)
df[['Name', 'Class'] ]= df_col
df

Q5. Write a solution to report all the duplicate emails in the data frame column.

data = [['abc@gmail.com', 'abc'], ['abc1@gmail.com', 'abc'], ['abc@gmail.com', 'abc'], ['xyz@gmail.com', 'xyz'],['xyz@gmail.com', 'xyz']]
df = pd.DataFrame(data, columns = ['email id', 'name'])

def redundant_emailid(df, email_column):
redt = df[email_column].value_counts()
redt = redt[redt>1]
return redt

redundant_emailid(df, 'email id')

Q6. How to check whether a dataframe is empty or not?

if df.empty:
print("DF is empty")

Q7. Write a code to convert the date in the string format to date format.

from datetime import datetime    

# Define dates as the strings
Day1 = 'Wednesday, July 14, 2018'
Day2 = '15/7/17'
Day3 = '16-07-2017'
print("Type of Day1 is :" + str(type(Day1)))

# Define dates as the datetime objects
dmy_dt1 = datetime.strptime(Day1, '%A, %B %d, %Y')
dmy_dt2 = datetime.strptime(Day2, '%d/%m/%y')
dmy_dt3 = datetime.strptime(Day3, '%d-%m-%Y')

#Print the converted dates
print(dmy_dt1, type(dmy_dt1))
print(dmy_dt2, type(dmy_dt2))
print(dmy_dt3, type(dmy_dt2))

Output:

Type of Day1 is :<class ‘str’>
2018–07–14 00:00:00 <class ‘datetime.datetime’>
2017–07–15 00:00:00 <class ‘datetime.datetime’>
2017–07–16 00:00:00 <class ‘datetime.datetime’>

Q8. Write a program to implement different types of merge on two DataFrames.

merge() provides flexibility in merging dataframe and is best suited for complex merging conditions.

Syntax of merge:

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html

Different types of merge:

https://datagy.io/pandas-merge-concat/
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [25, 30, 35, 40],
'Department': ['HR', 'Finance', 'IT', 'Marketing']
}
df1 = pd.DataFrame(data)

data2 = {
'Name': ['Alex', 'Bharat', 'Chaitra', 'Disha'],
'Age': [45, 32, 36, 47],
'Department': ['RnD', 'Finance', 'Tech', 'Marketing']
}
df2 = pd.DataFrame(data2)

df_inner = pd.merge(df1, df2, on='Department', how='inner')
df_inner
df_outer = pd.merge(df1, df2, on='Department', how='outer')
df_outer
df_left = pd.merge(df1, df2, on='Department', how='left')
df_left
df_right = pd.merge(df1, df2, on='Department', how='right')
df_right

Python Interview Prep: Questionnaire Guide Part 1

Python Interview Prep: Questionnaire Guide Part 2

EndNote:

Thanks for reading the blog. Have thoughts or questions? We’d love to hear from you! Feel free to leave a comment below.

Would love to catch you on Linkedin. Mail me here for any queries.

Stay tuned for more exciting content till then Happy reading!!!!

I believe in the power of continuous learning and sharing knowledge with the community. Your contributions are invaluable in helping me create meaningful content and resources that benefit everyone. Join me on this journey of exploration and innovation in the fascinating world of data science by donating to Buy Me a Coffee.

--

--