Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Analyzing Trends in News Coverage | Data Analysis and Visualization for Media
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Journalists and Media

bookAnalyzing Trends in News Coverage

Understanding how news coverage changes over time is essential for journalists who want to track the prominence of topics, events, or individuals. Time series analysis allows you to observe how often certain subjects appear in news articles, helping you identify patterns such as surges in interest, seasonal coverage, or gradual declines in attention. By analyzing temporal trends, you can uncover the narrative arc of a story, spot emerging issues, and provide readers with valuable context about how the news cycle evolves.

12345678910111213141516171819202122232425262728293031
import pandas as pd # Sample DataFrame of news articles with publication dates data = { "title": [ "Election Results Announced", "Mayor Launches New Initiative", "Protests Erupt Downtown", "City Council Approves Budget", "Community Garden Opens", "Election Campaigns Begin" ], "date_published": [ "2024-01-15", "2024-01-22", "2024-02-10", "2024-02-28", "2024-03-05", "2024-03-20" ] } df = pd.DataFrame(data) # Convert the 'date_published' column to datetime df["date_published"] = pd.to_datetime(df["date_published"]) # Group articles by month and count the number of articles per month monthly_counts = df.groupby(df["date_published"].dt.to_period("M")).size() print(monthly_counts)
copy

Grouping article publication dates by a specific time period, such as month, helps you visualize how coverage changes throughout the year. In the code above, you use pandas to convert the date column to datetime format and then group the data by month. This approach reveals patterns like spikes during elections or declines during quiet news periods. By counting articles per month, you can quickly spot when coverage of a topic intensified or faded, making it easier to explain trends and investigate underlying causes.

123456789101112
import matplotlib.pyplot as plt # Plotting the monthly article counts as a line chart monthly_counts = monthly_counts.sort_index() plt.figure(figsize=(8, 4)) monthly_counts.plot(kind="line", marker="o") plt.title("Number of News Articles Published Per Month") plt.xlabel("Month") plt.ylabel("Article Count") plt.grid(True) plt.tight_layout() plt.show()
copy

1. Why is it useful for journalists to analyze trends over time?

2. What pandas function can be used to group data by a time period?

3. Fill in the blank: To convert a column to datetime in pandas, use _____.

question mark

Why is it useful for journalists to analyze trends over time?

Select the correct answer

question mark

What pandas function can be used to group data by a time period?

Select the correct answer

question-icon

Fill in the blank: To convert a column to datetime in pandas, use _____.

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 2

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookAnalyzing Trends in News Coverage

Veeg om het menu te tonen

Understanding how news coverage changes over time is essential for journalists who want to track the prominence of topics, events, or individuals. Time series analysis allows you to observe how often certain subjects appear in news articles, helping you identify patterns such as surges in interest, seasonal coverage, or gradual declines in attention. By analyzing temporal trends, you can uncover the narrative arc of a story, spot emerging issues, and provide readers with valuable context about how the news cycle evolves.

12345678910111213141516171819202122232425262728293031
import pandas as pd # Sample DataFrame of news articles with publication dates data = { "title": [ "Election Results Announced", "Mayor Launches New Initiative", "Protests Erupt Downtown", "City Council Approves Budget", "Community Garden Opens", "Election Campaigns Begin" ], "date_published": [ "2024-01-15", "2024-01-22", "2024-02-10", "2024-02-28", "2024-03-05", "2024-03-20" ] } df = pd.DataFrame(data) # Convert the 'date_published' column to datetime df["date_published"] = pd.to_datetime(df["date_published"]) # Group articles by month and count the number of articles per month monthly_counts = df.groupby(df["date_published"].dt.to_period("M")).size() print(monthly_counts)
copy

Grouping article publication dates by a specific time period, such as month, helps you visualize how coverage changes throughout the year. In the code above, you use pandas to convert the date column to datetime format and then group the data by month. This approach reveals patterns like spikes during elections or declines during quiet news periods. By counting articles per month, you can quickly spot when coverage of a topic intensified or faded, making it easier to explain trends and investigate underlying causes.

123456789101112
import matplotlib.pyplot as plt # Plotting the monthly article counts as a line chart monthly_counts = monthly_counts.sort_index() plt.figure(figsize=(8, 4)) monthly_counts.plot(kind="line", marker="o") plt.title("Number of News Articles Published Per Month") plt.xlabel("Month") plt.ylabel("Article Count") plt.grid(True) plt.tight_layout() plt.show()
copy

1. Why is it useful for journalists to analyze trends over time?

2. What pandas function can be used to group data by a time period?

3. Fill in the blank: To convert a column to datetime in pandas, use _____.

question mark

Why is it useful for journalists to analyze trends over time?

Select the correct answer

question mark

What pandas function can be used to group data by a time period?

Select the correct answer

question-icon

Fill in the blank: To convert a column to datetime in pandas, use _____.

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 2
some-alt