Worksheets › Class 12 › Data Handling using Pandas – I › Cheat sheet
Class 12 Data Handling using Pandas – I: cheat sheet
CBSE Informatics Practices. pandas Series and DataFrames: making them, selecting with loc and iloc, filtering, changing and saving them.
Key ideas
- A Series is a 1-D labelled array (labels 0, 1, 2, … by default); a DataFrame is a 2-D table of labelled rows and columns.
- loc selects by label, slicing INCLUDING the end label; iloc selects by position from 0, end left out.
- df.loc['S2', 'Marks'] or df.iloc[1, 3] is one value; df.loc['S2'] is a row labelled by column names.
- head(n), tail(n): the first or last n rows (5 by default); df[df['Marks'] > 50] keeps the matching rows.
- Arithmetic on a Series works element by element and keeps the labels; pd.Series(5, index=[…]) repeats 5.
- df['Total'] = df['A'] + df['B'] adds a column; df.drop(label) drops a row, df.drop(col, axis=1) a column.
- shape is (rows, columns) and size is rows × columns; pd.read_csv() reads a CSV file, df.to_csv() writes one.
Remember
| Labels a to f | s.loc['b':'d'] → b, c, d; s.iloc[1:3] → b, c |
| Some columns | df[['Name', 'Maths']] (two pairs of brackets) |
| shape (r, c), size r × c | add a column: c + 1; drop a row: r − 1; drop a column: c − 1 |
Watch out
- Leaving out the end label in a loc slice, or keeping the end in an iloc slice.
- Forgetting axis=1 when dropping a column: drop() removes a row by default.
Worked example
Series s: a | 38 b | 96 c | 75 d | 29 e | 61 f | 56 Write the output of each statement, and say why they differ. (a) print(s.loc['b':'f']) (b) print(s.iloc[1:5])
Working: loc slices by label (end included); iloc by position (end left out)
Answer: (a) b | 96 c | 75 d | 29 e | 61 f | 56 (b) b | 96 c | 75 d | 29 e | 61 loc includes the end label f; iloc stops before position 5.
Practise Data Handling using Pandas – I
Revise with this page, then practise: every Data Handling using Pandas – I sheet in the app is new, with an answer key and working.
Coming soon for Android and iPhone.