Mathew K Analytics

Lesson 18 · Social Media Content Analytics

Handling Duplicate Content Records in Social Media Analytics

Duplicates can distort engagement metrics and hide insights. This lesson shows how to detect, explore, and resolve duplicate records in real datasets.…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Handling Duplicate Content Records in Social Media Analytics#

  • Duplicates can distort engagement metrics and hide insights.
  • This lesson shows how to detect, explore, and resolve duplicate records in real datasets.
  • Accurate analytics help creators and businesses make better content decisions.
  • We will use public social media datasets such as YouTube Trending Videos and synthetic feeds.
  • By the end, you will know how to clarify metrics for true, actionable reporting.
import pandas as pd
import numpy as np
import os
import warnings
warnings.filterwarnings('ignore')

Core Concepts: Social Media Content Data and Engagement#

  • Social media datasets capture posts, videos, engagement, and timing.
  • Metrics like views, likes, comments, and watch time show true audience response.
  • Duplicates happen due to re-publishing, scraping, or sync issues.
  • Beginners sometimes double-count engagement or group incorrectly.
  • Correct removal of duplicates is vital for honest benchmarks.
# Load the Social Media Content Dataset (synthetic data with reproducibility)
np.random.seed(42)
n_posts = 500
views = np.random.randint(100, 100000, n_posts)
df = pd.DataFrame({
    'post_id': range(1, n_posts+1),
    'platform': np.random.choice(['YouTube','Instagram','TikTok'], n_posts),
    'date': pd.date_range('2023-01-01', periods=n_posts, freq='6h'),
    'views': views,
    'likes': (views * np.random.uniform(0.02, 0.15, n_posts)).astype(int),
    'comments': (views * np.random.uniform(0.001, 0.05, n_posts)).astype(int),
    'shares': (views * np.random.uniform(0.001, 0.03, n_posts)).astype(int)
})
print(df.head(3))
print(df.shape)
   post_id   platform                date  views  likes  comments  shares
0        1  Instagram 2023-01-01 00:00:00  15895   2356       116     253
1        2  Instagram 2023-01-01 06:00:00    960     94        16      26
2        3  Instagram 2023-01-01 12:00:00  76920   3910      2879    1185
(500, 7)
# Let us quickly check the columns and missing values
print(df.columns.tolist())
print(df.isnull().sum())
['post_id', 'platform', 'date', 'views', 'likes', 'comments', 'shares']
post_id     0
platform    0
date        0
views       0
likes       0
comments    0
shares      0
dtype: int64
# Let us intentionally introduce duplicate content records to simulate real-world issues
dupes = df.sample(frac=0.05, random_state=42)
df_with_dupes = pd.concat([df, dupes], ignore_index=True)
print(f'Total rows after inserting duplicates: {df_with_dupes.shape[0]}')
Total rows after inserting duplicates: 525
# BEGINNER 1: Count duplicated rows in the dataset
num_duplicates = df_with_dupes.duplicated().sum()
print(f'Number of fully duplicated rows: {num_duplicates}')
Number of fully duplicated rows: 25
# BEGINNER 2: Preview the first few duplicate rows
duplicates = df_with_dupes[df_with_dupes.duplicated()]
print(duplicates.head(2))
     post_id platform                date  views  likes  comments  shares
500      362   TikTok 2023-04-01 06:00:00  47302   6630       862    1332
501       74  YouTube 2023-01-19 06:00:00  92193   6144      3036     441
# BEGINNER 3: Remove all fully duplicate rows
df_nodup = df_with_dupes.drop_duplicates()
print(f'Rows remaining after removal: {df_nodup.shape[0]}')
Rows remaining after removal: 500
# BEGINNER 4: Check if post_id is unique after removing all duplicates
postid_unique = df_nodup['post_id'].is_unique
print(f'post_id uniqueness: {postid_unique}')
post_id uniqueness: True
# BEGINNER 5: Remove duplicates based on only 'post_id', keeping the most recent date
df_latest = df_with_dupes.sort_values('date').drop_duplicates('post_id', keep='last')
print(df_latest[['post_id','platform','date']].head(3))
   post_id   platform                date
0        1  Instagram 2023-01-01 00:00:00
1        2  Instagram 2023-01-01 06:00:00
2        3  Instagram 2023-01-01 12:00:00
# INTERMEDIATE 1: Find posts duplicated by content columns (ignoring post_id)
dupe_columns = ['platform','date','views','likes','comments','shares']
partial_dupes = df_with_dupes.duplicated(subset=dupe_columns)
print(f'Partial duplicates based on engagement and time: {partial_dupes.sum()}')
Partial duplicates based on engagement and time: 25
# INTERMEDIATE 2: Visualize number of duplicates by platform
dupe_counts = df_with_dupes[df_with_dupes.duplicated('post_id', keep=False)].groupby('platform').size()
print('Duplicate counts by platform:')
print(dupe_counts)
Duplicate counts by platform:
platform
Instagram    20
TikTok       16
YouTube      14
dtype: int64
# INTERMEDIATE 3: Aggregate total likes and comments per post with and without duplicates
engagement_original = df_with_dupes.groupby('post_id')[['likes','comments']].sum().sum()
engagement_nodup = df_nodup.groupby('post_id')[['likes','comments']].sum().sum()
print(f'Total likes+comments (with dupes): {engagement_original}')
print(f'Total likes+comments (no dupes): {engagement_nodup}')
Total likes+comments (with dupes): likes       2274641
comments     684207
dtype: int64
Total likes+comments (no dupes): likes       2192816
comments     657383
dtype: int64
# INTERMEDIATE 4: Detect duplicated engagement for the same post_id
dupe_postids = df_with_dupes['post_id'][df_with_dupes.duplicated('post_id')]
print('Sample duplicated post IDs:')
print(dupe_postids.head(5).tolist())
Sample duplicated post IDs:
[362, 74, 375, 156, 105]
# INTERMEDIATE 5: Compare engagement distributions before and after deduplication
import matplotlib.pyplot as plt
plt.figure(figsize=(10,4))
df_with_dupes['likes'].hist(alpha=0.5, label='With Duplicates')
df_nodup['likes'].hist(alpha=0.5, label='No Duplicates')
plt.legend()
plt.xlabel('Likes per row')
plt.ylabel('Frequency')
plt.title('Likes Distribution With and Without Duplicates')
plt.tight_layout()
plt.savefig('likes_hist.png')
plt.show()
No description has been provided for this image
 

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.