Seminar 005: Working with Pandas¶
Notes¶
Working with Pandas¶
import pandas as pd
# Fetch a dataset from the web
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"
You can display a dataset as a beautiful table inside a Jupyter Notebook by writing the variable name as shown below.
# Dataframe has to be the last line and without a print statement
Data analysis¶
Pandas alone already provides many options to analyse your data. You can calculate various statistics and even plot data (although not visually pleasing). Most of the time, when working with numerical data, using the df.describe() method already gives a great overview.
# General description
df.describe()
# Correlation matrix (Pearson)
df.corr("pearson", numeric_only=True)
# Correlation matrix (Spearman)
df.corr("spearman", numeric_only=True)
Slicing and conditional sorting¶
Pandas provides a convenient way to extract specific data points by using logical operators within square brackets.
# Only extract data for "setosa" species
# Filter data to only include rows where sepal_length is greater than 7 and petal_length is greater than 6
Visualisation¶
For a quick visualization of data, Pandas can be sufficient, but bear in mind that the visualizations are basic. On the other hand, Pandas is capable of Parallel Coordinates, which is very powerful!
# Short-hand box plot from a dataframe.plot
# Multi-dimensional plotting (Parallel coordinates)
from pandas.plotting import parallel_coordinates
Exporting data¶
Aside from csv Pandas can also read XLSX (Excel) and many other formats to load and write a dataset.
# Viewable in the browser
df.to_html("Dataset.html")
# Well known file format
df.to_xml("Dataset.xml")
# Can be used and edited in Excel
df.to_excel("Dataset.xlsx", index=False)
# ... and can be read again
pd.read_excel("Dataset.xlsx", sheet_name="Sheet1")