liveBook · Manning

Pandas

As explained in Starting Data Analytics with Generative AI and Python

pandas is a Python library providing labeled data structures and tools for manipulating and analyzing tabular data.

pandas is a Python library for working with tabular data in DataFrames. It can help answer practical questions about data quality, customer activity, and review content before those findings inform a decision. section 2.3 section 5.5.6

Start by inspecting a DataFrame before cleaning or analyzing it:

  • Use df.head() and df.shape to see sample rows and the number of rows and columns. section 2.3
  • Use df.info() to inspect data types and non-null counts, and df.describe() for numerical summaries. section 2.3
  • Use df.isna().sum() to count missing values by column, and value_counts() to inspect frequencies in a categorical column. section 2.3

Check customer identifiers

A customer dataset may contain both customer_id and customer_unique_id. Count distinct values with nunique(), then group by customer_unique_id and count distinct customer_id values to see how the identifiers relate. Inspect the results before assuming that each row represents a different customer. section 2.3

import pandas as pd

# Sample DataFrame
data = {'customer_id': [1, 2, 3, 4, 5],
        'customer_unique_id': [1001, 1002, 1002, 1004, 1004]}

df = pd.DataFrame(data)

# Check the number of unique values in each column
unique_customer_ids = df['customer_id'].nunique()
unique_customer_unique_ids = df['customer_unique_id'].nunique()

print(f"Unique customer_id values: {unique_customer_ids}")
print(f"Unique customer_unique_id values: {unique_customer_unique_ids}")

# Check the relationship between the two columns
relationship = df.groupby('customer_unique_id')['customer_id'].nunique()

print("\nRelationship between customer_unique_id and customer_id:")
print(relationship)

This small DataFrame demonstrates distinct counts and grouping for two customer identifiers. section 2.3

Find reviews with positive keywords

For Portuguese customer reviews, define a list of positive words, apply a matching function to the review column, and filter rows marked true. A keyword match is only a basic signal: a review can contain a positive word and still express a complaint. Inspect sample reviews and address missing values before relying on the output. section 5.5.6

import pandas as pd
# Load your dataframe
df = pd.read_csv('olist_order_reviews_dataset.csv')

# List of positive keywords in Portuguese
positive_keywords = ['ótimo', 'excelente', 'bom', 'incrível', 
'maravilhoso', 'perfeito', 'gostei', 'satisfeito', 'recomendo', 
'amei']

# Define a function to check if a review contains positive keywords
def is_positive(review):
    for keyword in positive_keywords:
        if keyword.lower() in review.lower():
            return True
    return False

# Apply the function to the 'review_comment_message' column
df['is_positive'] = df['review_comment_message'].apply(is_positive)

# Create a new dataframe with only positive reviews
positive_reviews_df = df[df['is_positive']]

# Save the positive reviews to a new CSV file
positive_reviews_df.to_csv('positive_reviews.csv', index=False)

This workflow reads reviews from CSV, marks keyword matches, filters the DataFrame, and writes the result to another CSV. section 5.5.6

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Demonstrates inspection, grouping, and filtering with worked code; the review search remains basic and leaves missing-value handling for further work.
Examples
Examines customer identifier relationships and filters Portuguese customer reviews using positive keywords.
What you can do after
Apply pandas to inspect DataFrames, compare identifier counts, and produce a basic keyword-filtered review dataset.
How it connects
Connects DataFrames with data cleaning, exploratory data analysis, keyword search, and text mining.

Also explained in

Compare how 11 books explain pandas →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.