Pandas: CSV Import/Export, Cleaning, Joining and Descriptive Statistics
What this domain tests
This domain tests the full small-data workflow: read a CSV into a DataFrame, inspect and clean it, combine two tables, summarise it with statistics, and write results back to disk.
Import and export
pd.read_csv('data.csv')treats the first row as column headers and uses a comma separator by default. Useful arguments aresep,header=None,names=[...],index_col,usecols,nrows,na_valuesanddtype.df.to_csv('out.csv', index=False)prevents the row index from being written as an extra unnamed column.
Cleaning and joining
- Inspect with
head(),tail(),shape,info(),isnull().sum()andduplicated().sum(). - Clean with
dropna(how='any'/'all', subset=[...]),fillna(value),drop_duplicates(subset=..., keep='first'),rename(columns={...}),astype()andstr.strip(). pd.merge(left, right, on='key', how='inner'/'left'/'right'/'outer')combines tables on a shared column;pd.concat([df1, df2], axis=0)stacks rows andaxis=1places columns side by side.
Descriptive statistics
df.describe() reports count, mean, standard deviation, minimum, quartiles and maximum for numeric columns only; add include='all' to include text columns. Series methods such as mean(), median(), mode(), value_counts() and corr() give finer summaries.
Worked example
A students table has Roll and Name; a marks table has Roll and Marks. pd.merge(students, marks, on='Roll', how='left') keeps every student, filling NaN for anyone with no marks. With how='inner' those students would disappear. Reasoning: the join key matches rows; the join type decides which unmatched rows survive.
Traps that cost marks
Forgetting index=False in to_csv; assuming describe() covers text columns; confusing merge with concat; expecting dropna() to remove only fully empty rows; filling missing values without checking whether the mean is a sensible substitute.