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 column
For 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.
More Related questions...