Are you ready to delve deeper into the core functionalities of Pandas, one of the most popular Python libraries in data science? Today, we will focus on learning about Pandas DataFrame and data manipulation — the backbone of many data operation tasks. We're going to handle the Titanic dataset — a fascinating example containing real-world data — that will keep you engaged as we navigate through the lesson.
Pandas DataFrames bring versatility and power to the table when it comes to data manipulation. Think of it as Excel, but on steroids, capable of handling large and complex datasets. The processing abilities of DataFrames are crucial for cleaning, transforming, and analyzing datasets in earnest.
Say you've got a dataset, like the Titanic, but some data is missing. Or perhaps there are some anomalies you’d want to filter out. Or you need specific segments of data to examine a particular hypothesis. How would you do it? By mastering Pandas DataFrames, you'd be well-equipped to tackle these tasks!
Pandas DataFrame is a two-dimensional labeled data structure capable of holding data of various types—integers, floats, strings, Python objects, and more. It's generally the most commonly used Pandas object.
Let's start simply by creating a DataFrame from a dictionary:
Each key-value pair in the dictionary corresponds to a column in the resulting DataFrame. The key defines the column label, and the corresponding value is a list of column values. The DataFrame constructor takes a dictionary as input and turns it into a two-dimensional table where keys become column names, and values in each key (which should be a list) will be the values for the respective column. Here "John", "Anna", and "Peter" have ages 28, 24, and 33, respectively, and they live in "New York", "Los Angeles", and "Berlin".
To inspect the structure and properties of a DataFrame, we have a range of functions at our disposal. Here are some commonly used ones:
df.head(n): Returns the firstnrows of theDataFrame df.df.tail(n): Returns the lastnrows of theDataFrame df.df.shape: Returns a tuple representing the dimensions(number_of_rows, number_of_columns)of theDataFrame df.df.columns: Returns an index containing column labels of theDataFrame df.df.dtypes: Returns a series with the data type of each column.
Let's see these functions in action below:
These commands will help us understand the basic shape and type of the DataFrame. df.head(2) prints the first 2 rows of the DataFrame, which are {[John, 28, New York], [Anna, 24, Los Angeles]}. df.tail(2) prints the last 2 rows which are {[Anna, 24, Los Angeles}, [Peter, 33, Berlin]}. df.shape gives us the dimensions of the DataFrame here being (3,3) indicating 3 rows and 3 columns. df.columns prints the column names [Name, Age, City] and df.dtypes gives us the data types in each column.
