Basic Data Analysis

Topic Overview

Hey there! Curious about data's hidden secrets? Today, we dive into Basic Data Analysis—an essential step for data comprehension. We unearth patterns, and guide decision-making across various fields, be it business, science, or daily life, with a powerful tool—the pandas Python library. Let's embark on this journey!

Meaning of Basic Data Analysis

Rising to the challenge of solving a data mystery, Basic Data Analysis serves as the groundwork. It encompasses understanding and decision-making—be it a business owner understanding customer behavior, a scientist analyzing research data, or a student making sense of study material. With pandas, this process becomes effortless.

Using `value_counts()` for Frequency Analysis

Firstly, we employ value_counts(), a method that swiftly counts the frequency of DataFrame elements. Consider an imaginary dataset of pets.

import pandas as pd

# Creating DataFrame
data =  {'Name': ['Tommy', 'Rex', 'Bella', 'Charlie', 'Lucy', 'Cooper'],
         'Type': ['Dog', 'Dog', 'Cat', 'Cat', 'Dog', 'Bird']}
pets_df = pd.DataFrame(data)

Using the value_counts() function we can count count unique elements of a series (a dataframe column):

print(pets_df['Type'].value_counts())
# Output:
# Dog     3
# Cat     2
# Bird    1
# Name: Type, dtype: int64

With value_counts(), establishing frequency distribution in series becomes straightforward.

Grouping and Aggregating with `groupby()` and `agg()` methods

For summarizing data, groupby() and agg() prove useful! Now let’s add weight to the pets in our DataFrame to illustrate these methods:

import pandas as pd

# Creating DataFrame
data =  {'Name': ['Tommy', 'Rex', 'Bella', 'Charlie', 'Lucy', 'Cooper'],
         'Type': ['Dog', 'Dog', 'Cat', 'Cat', 'Dog', 'Bird'],
         'Weight': [12, 15, 8, 9, 14, 1]}
pets_df = pd.DataFrame(data)

Now group and calculate the mean of data based on pet type.

print(pets_df.groupby('Type').agg({'Weight': 'mean'}))
  1. groupby('Type'): Splits the data into groups based on 'Type'.
  2. .agg({'Weight': 'mean'}): Applies the 'mean' function to the 'Weight' column for each group.

The resulting DataFrame shows the average weight for each pet type:

  • Bird: 1.0
  • Cat: 8.5
  • Dog: 13.67

Of course, calculating mean is not the only option. We can use functions like min, max, median, etc. We will talk more about using different aggregation functions in the next course.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal