import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
# 假设的网站用户访问日志数据(可替换为真实数据)
data = {
'User ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'Visit Time': ['2023-01-01 10:05:00', '2023-01-01 11:20:00', '2023-01-01 14:15:00', '2023-01-02 09:30:00',
'2023-01-02 13:45:00', '2023-01-02 16:20:00', '2023-01-03 12:10:00', '2023-01-03 15:30:00',
'2023-01-03 17:45:00', '2023-01-04 11:15:00'],
'Page Visited': ['Home', 'Product Page', 'Checkout', 'Home', 'Product Page', 'Search', 'Home', 'Product Page',
'Checkout', 'Search']
}
# 将数据转换为 DataFrame
df = pd.DataFrame(data)
df['Visit Time'] = pd.to_datetime(df['Visit Time'])
# 按日期统计访问量
daily_visits = df.groupby(df['Visit Time'].dt.date).size()
# 按页面统计访问量
page_visits = df.groupby('Page Visited').size()
# 绘制每日访问量折线图
plt.plot(daily_visits.index, daily_visits.values)
plt.xlabel('Date')
plt.ylabel('Number of Visits')
plt.title('Daily Website Visits')
plt.xticks(rotation=45)
plt.show()
# 绘制页面访问量柱状图
plt.bar(page_visits.index, page_visits.values)
plt.xlabel('Page')
plt.ylabel('Number of Visits')
plt.title('Page Visits Distribution')
plt.xticks(rotation=45)
plt.show()
# 找出{BANNED}最佳热门的页面
most_popular_page = page_visits.idxmax()
print(f"The most popular page is: {most_popular_page}")