Data Handling Using Pandas
Section B2, Informatics Practices. Covers Python libraries for data; Series; DataFrames; Indexing, merging and CSV; Descriptive statistics; Aggregation, grouping and pivoting; Missing values and MySQL.
About this unit
This unit introduces data analysis in Python with the Pandas library, supported by NumPy. You will create Series (one-dimensional) and DataFrames (two-dimensional) from lists, dictionaries and CSV files, select and change rows and columns, filter data with conditions, and join tables. You will then summarise data with statistical functions, group and pivot it, handle missing values, and move data between Pandas and a MySQL database.
Key ideas
- NumPy arrays hold elements of one type and support fast element-wise maths.
- A Series is a labelled one-dimensional array; a DataFrame is a labelled two-dimensional table.
- loc selects by labels (end label included); iloc selects by positions (end excluded).
- Boolean indexing such as df[df['Marks'] > 60] filters rows.
- merge(), concat(), read_csv() and to_csv() combine, import and export data.
- Functions such as sum(), mean(), median(), mode(), quantile(), var() and std() summarise data; groupby() and pivot() reshape it.
- Missing values (NaN) are found with isnull() and handled with dropna() or fillna().
How to prepare
Type the examples into Python and check the output yourself; most questions show short code and ask for the result, shape or number of rows. Learn the default axis of each function (columns, axis = 0) and remember that Pandas statistics skip NaN values.
Topics in this chapter
Python libraries for data
Pandas, NumPy and Matplotlib.
5 questionsSeries
Creating a Series and indexing, slicing and maths on it.
5 questionsDataFrames
Creating DataFrames from dictionaries, lists and CSV files; selecting, adding, deleting and renaming rows and columns.
5 questionsIndexing, merging and CSV
Label and Boolean indexing, joining, merging, concatenation and CSV import and export.
5 questionsDescriptive statistics
max, min, count, sum, mean, median, mode, quartiles, variance and standard deviation.
5 questionsAggregation, grouping and pivoting
Group by, sorting, pivoting and reindexing.
5 questionsMissing values and MySQL
Dropping and filling missing values; moving data between MySQL and Pandas.
5 questions