Python / Data Science Essentials Interview Questions
How do you read CSV, Excel, and JSON files into a Pandas DataFrame?
Pandas has a family of pd.read_* functions that handle virtually every common data format. Getting data in is usually the first step of any data science workflow, so these functions deserve close attention.
import pandas as pd
# --- CSV ---
df = pd.read_csv('sales.csv')
# Common options:
df = pd.read_csv(
'sales.csv',
sep=';', # custom delimiter (semicolon, tab, etc.)
header=0, # row to use as column names (0 = first row)
index_col='order_id', # use this column as the row index
usecols=['date', 'amount', 'region'], # read only these columns
dtype={'amount': 'float32'}, # explicit dtype
parse_dates=['date'], # auto-parse date strings
na_values=['N/A', '--', ''], # treat as NaN
nrows=1000, # read only first 1000 rows (useful for large files)
encoding='utf-8',
)
# --- Excel ---
df_xl = pd.read_excel('report.xlsx', sheet_name='Q1', skiprows=2)
# --- JSON ---
df_j = pd.read_json('data.json', orient='records')
# orient='records' expects [{...}, {...}] — the common API response shape
# --- SQL ---
import sqlite3
conn = sqlite3.connect('mydb.sqlite')
df_sql = pd.read_sql('SELECT * FROM orders WHERE amount > 100', conn)
# Always inspect after reading
print(df.shape)
print(df.head())
print(df.dtypes)
print(df.info()) # shows non-null counts per columnFor very large CSVs that do not fit in memory, pass chunksize=100_000 to read_csv — it returns an iterator of DataFrames, each containing that many rows. Process and aggregate chunk by chunk without loading the full file.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
