Indexing, merging and CSV
Label and Boolean indexing, joining, merging, concatenation and CSV import and export.
The essentials
Pandas offers several ways to select data: by label, by position and by condition. It can also combine DataFrames and read or write data as CSV (comma-separated values) files, which store a table as plain text with one row per line.
Key points
- loc (label based): df.loc['r2'] gives a row; df.loc['r2':'r3'] includes both ends; df.loc['r1', 'Marks'] gives one value; df.loc[:, 'Name'] gives a column.
- iloc (position based): df.iloc[1:3] gives rows at positions 1 and 2 (the end is excluded); df.iloc[0, 1] gives one value.
- Boolean indexing: df[df['Marks'] > 60] keeps rows where the condition is True. Conditions can be combined with & (and) and | (or), each in brackets.
- df.loc[df['Marks'] > 60, 'Name'] gives names of matching rows.
- concat: pd.concat([df1, df2]) joins DataFrames one below the other (axis=0) or side by side (axis=1).
- merge: pd.merge(df1, df2, on='id') joins on a common column, like an SQL join. By default it keeps only matching rows (inner join); how='outer', 'left' or 'right' keep others too.
- join: df1.join(df2) joins on the index.
- Import: pd.read_csv('data.csv') reads a CSV file; sep sets the separator; header=None means the file has no header row; names sets column names.
- Export: df.to_csv('out.csv') writes the DataFrame; index=False leaves out the row labels; sep sets a different separator.
Exam tips
- loc includes the end label; iloc excludes the end position.
- For merge questions, count only the key values present in both DataFrames for an inner join.
Common mistakes
- Writing df[df['Marks'] > 60 and df['Age'] < 18]; Pandas needs & with brackets.
- Forgetting index=False and finding an extra unnamed column when the file is read back.
Practice questions
Log in to answer these questions and see the explanations.Create free accountLog in