Python_Questions__BA
Python_Questions__BA
Q: How do you load a large CSV file using Python and filter rows based on a condition?
A: Use pandas:
'''python
import pandas as pd
df = pd.read_csv('data.csv')
'''
'''python
df.iloc[1] # by position
'''
Q: How would you group data by a column and calculate aggregate metrics like sum or mean?
A: '''python
df.groupby('category_column')['value_column'].sum()
df.groupby('category_column')['value_column'].mean()
'''
Q: How can you merge two dataframes in Pandas? What types of joins are available?
'''python
'''
Q: Explain how you would create a new column based on conditions from other columns.
A: '''python
'''
Q: Can you pivot and unpivot a table using Pandas? When would you do that?
'''python
df.melt(id_vars=['id'])
'''
A: '''python
'''
Q: How would you convert a column from string to datetime, and extract parts like day, month?
A: '''python
df['date'] = pd.to_datetime(df['date_str'])
df['day'] = df['date'].dt.day
'''
Q: How can you detect and remove outliers using IQR or Z-score in Python?
A: '''python
Q1 = df['col'].quantile(0.25)
Q3 = df['col'].quantile(0.75)
IQR = Q3 - Q1
filtered = df[(df['col'] >= Q1 - 1.5 * IQR) & (df['col'] <= Q3 + 1.5 * IQR)]
'''
Q: You're asked to replicate a SQL GROUP BY + HAVING clause in Pandas. How would you do it?
A: '''python
'''
Q: How can you perform a window function (like moving average) in Pandas?
A: '''python
df['moving_avg'] = df['value'].rolling(window=3).mean()
'''
Q: Have you ever automated a reporting process in Python? How did you schedule or trigger it?
A: Use 'schedule', 'APScheduler', or cron jobs. Write a script and schedule it with cron (Linux) or Task
Scheduler (Windows).
Q: How would you connect to a SQL database and run a query using Python?
A: '''python
import mysql.connector
conn = mysql.connector.connect(...)
cursor = conn.cursor()
'''
Q: How would you send automated email reports or alert messages from a Python script?
'''python
import smtplib
'''
A: '''python
'''
A: '''python
import json
with open('file.json') as f:
data = json.load(f)
print(data['key'])
'''
'''python
'''
Q: A dashboard shows drop in user renewals. How will you use Python to investigate the reason?
A: Analyze churn data, compare previous periods, check feature usage, demographics, and user activity.
Q: You notice a spike in app installs but no rise in purchases. What Python scripts would you run to
Q: You need to identify which car brand's insurance sales dropped in the last month. How will you do
A: Query monthly sales by brand using SQL, load into Pandas, and compare month-over-month sales using