Understanding DataFrames
Lesson 4 of 19 in Coddy's Pandas Analytics course.
DataFrames form the heart of the Pandas library. A DataFrame is a two-dimensional labeled data structure with columns that can be of different types, including integer, float, object, and more.
In essence, a DataFrame is like a table of data. You can imagine it as a spreadsheet or SQL table. Rows correspond to individual input instances (observations), while columns represent the features of the data. With dataframes, we can manipulate, analyze, group, and preprocess data.
When working with a new dataframe, we need to understand what the columns are, what their datatypes are, how many rows it has, what kind of data it stores, and so on. Here are a few functions that provide quick insights about the data:
- head(n): Shows the first
nrows - head(): Shows the first 5 rows
- describe(): Provide descriptive statistics such as mean, max, min, etc.
- info(): Gives a summary of all features (columns).
shape:the shape of the dataframe (how many rows and how many columns). This is a property and not a method.- tail(n): Show the last
nrows - tail(): Shows the first 5 rows
Challenge
EasyIn this challenge, you have a CSV file called brands.csv.
Before doing anything, use the .read_csv() to transform the file into a dataframe.
Create a function named information that accepts a string that can hold one of the following values:
- "head"
- "describe"
- "info"
- "shape"
- "tail"
The function will print the corresponding output of the dataframe.
Try it yourself
import pandas as pd
def information(action):
df = pd.read_csv("brands.csv")
# Write code hereAll lessons in Pandas Analytics
4Data Analysis with Pandas
Descriptive StatisticsGrouping and Aggregating DataDifferent AggregationsMerge & Concat2Working with the DataFrame
Understanding DataFramesAccessing DataData Cleaning - Missing dataData Cleaning - More tools3Data Manipulation with Pandas
Return Requested ResultFilter DataAdd & DeleteModify DataModify StringsCustom Modifications