需求一数据分析及可视化一、商户分析1找出美国最常见商户前202找出美国商户最多的前10个城市3找出美国商户最多的前5个州4找出美国最常见商户并显示平均评分前205统计评分最高的城市前106统计category的数量7统计最多的category及数量前108收获五星评论最多的商户前209统计不同类型中国菜、美式、墨西哥的餐厅类型及数量10统计不同类型中国菜、美式、墨西哥的餐厅的评论数量11统计不同类
以下是使用Python进行可视化的示例代码:
商户分析:
- 找出美国最常见商户(前20)
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 统计商户数量
merchant_counts = df['merchant'].value_counts().head(20)
# 可视化
plt.bar(merchant_counts.index, merchant_counts.values)
plt.xlabel('Merchant')
plt.ylabel('Count')
plt.title('Top 20 Most Common Merchants in the US')
plt.xticks(rotation=90)
plt.show()
- 找出美国商户最多的前10个城市
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 统计城市商户数量
city_merchant_counts = df['city'].value_counts().head(10)
# 可视化
plt.bar(city_merchant_counts.index, city_merchant_counts.values)
plt.xlabel('City')
plt.ylabel('Count')
plt.title('Top 10 Cities with the Most Merchants in the US')
plt.xticks(rotation=90)
plt.show()
- 找出美国商户最多的前5个州
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 统计州商户数量
state_merchant_counts = df['state'].value_counts().head(5)
# 可视化
plt.bar(state_merchant_counts.index, state_merchant_counts.values)
plt.xlabel('State')
plt.ylabel('Count')
plt.title('Top 5 States with the Most Merchants in the US')
plt.xticks(rotation=90)
plt.show()
- 找出美国最常见商户,并显示平均评分(前20)
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 根据商户分组,计算平均评分
merchant_avg_rating = df.groupby('merchant')['rating'].mean().sort_values(ascending=False).head(20)
# 可视化
plt.bar(merchant_avg_rating.index, merchant_avg_rating.values)
plt.xlabel('Merchant')
plt.ylabel('Average Rating')
plt.title('Top 20 Most Common Merchants in the US with Average Rating')
plt.xticks(rotation=90)
plt.show()
- 统计评分最高的城市(前10)
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 根据城市分组,计算平均评分
city_avg_rating = df.groupby('city')['rating'].mean().sort_values(ascending=False).head(10)
# 可视化
plt.bar(city_avg_rating.index, city_avg_rating.values)
plt.xlabel('City')
plt.ylabel('Average Rating')
plt.title('Top 10 Cities with the Highest Average Rating in the US')
plt.xticks(rotation=90)
plt.show()
- 统计category的数量
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 统计category数量
category_counts = df['category'].nunique()
# 可视化
plt.bar('Category', category_counts)
plt.xlabel('Category')
plt.ylabel('Count')
plt.title('Number of Categories')
plt.show()
- 统计最多的category及数量(前10)
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 统计category数量
category_counts = df['category'].value_counts().head(10)
# 可视化
plt.bar(category_counts.index, category_counts.values)
plt.xlabel('Category')
plt.ylabel('Count')
plt.title('Top 10 Most Common Categories in the US')
plt.xticks(rotation=90)
plt.show()
- 收获五星评论最多的商户(前20)
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 筛选出五星评论
five_star_reviews = df[df['rating'] == 5]
# 统计商户数量
merchant_counts = five_star_reviews['merchant'].value_counts().head(20)
# 可视化
plt.bar(merchant_counts.index, merchant_counts.values)
plt.xlabel('Merchant')
plt.ylabel('Count')
plt.title('Top 20 Merchants with the Most Five-star Reviews')
plt.xticks(rotation=90)
plt.show()
- 统计不同类型(中国菜、美式、墨西哥)的餐厅类型及数量
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 筛选出中式、美式、墨西哥餐厅
cuisine_counts = df[df['category'].isin(['Chinese', 'American', 'Mexican'])]['category'].value_counts()
# 可视化
plt.bar(cuisine_counts.index, cuisine_counts.values)
plt.xlabel('Cuisine')
plt.ylabel('Count')
plt.title('Number of Restaurants by Cuisine Type')
plt.show()
- 统计不同类型(中国菜、美式、墨西哥)的餐厅的评论数量
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 筛选出中式、美式、墨西哥餐厅
cuisine_reviews = df[df['category'].isin(['Chinese', 'American', 'Mexican'])].groupby('category')['review_count'].sum()
# 可视化
plt.bar(cuisine_reviews.index, cuisine_reviews.values)
plt.xlabel('Cuisine')
plt.ylabel('Review Count')
plt.title('Total Review Count by Cuisine Type')
plt.show()
- 统计不同类型(中国菜、美式、墨西哥)的餐厅的评分分布
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 筛选出中式、美式、墨西哥餐厅
chinese_ratings = df[df['category'] == 'Chinese']['rating']
american_ratings = df[df['category'] == 'American']['rating']
mexican_ratings = df[df['category'] == 'Mexican']['rating']
# 可视化
plt.hist(chinese_ratings, bins=5, alpha=0.5, label='Chinese')
plt.hist(american_ratings, bins=5, alpha=0.5, label='American')
plt.hist(mexican_ratings, bins=5, alpha=0.5, label='Mexican')
plt.xlabel('Rating')
plt.ylabel('Count')
plt.title('Rating Distribution by Cuisine Type')
plt.legend()
plt.show()
用户分析:
- 分析每年加入的用户数量
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 转换日期格式
df['year'] = pd.to_datetime(df['join_date']).dt.year
# 统计每年加入的用户数量
user_counts = df['year'].value_counts().sort_index()
# 可视化
plt.plot(user_counts.index, user_counts.values)
plt.xlabel('Year')
plt.ylabel('User Count')
plt.title('Number of Users Joined per Year')
plt.show()
- 统计评论达人(review_count)
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 统计评论达人数量
review_counts = df['review_count'].value_counts().sort_index()
# 可视化
plt.bar(review_counts.index, review_counts.values)
plt.xlabel('Review Count')
plt.ylabel('User Count')
plt.title('Number of Users by Review Count')
plt.show()
- 统计人气最高的用户(fans)
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 统计人气最高的用户数量
top_fans = df['fans'].value_counts().head(10)
# 可视化
plt.bar(top_fans.index, top_fans.values)
plt.xlabel('Number of Fans')
plt.ylabel('User Count')
plt.title('Top 10 Users with the Most Fans')
plt.show()
- 统计每年优质用户,普通用户比例
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 转换日期格式
df['year'] = pd.to_datetime(df['join_date']).dt.year
# 统计每年优质用户和普通用户数量
elite_counts = df[df['elite'] != 'None']['year'].value_counts().sort_index()
non_elite_counts = df[df['elite'] == 'None']['year'].value_counts().sort_index()
# 可视化
plt.plot(elite_counts.index, elite_counts.values, label='Elite Users')
plt.plot(non_elite_counts.index, non_elite_counts.values, label='Non-Elite Users')
plt.xlabel('Year')
plt.ylabel('User Count')
plt.title('Number of Elite Users vs Non-Elite Users per Year')
plt.legend()
plt.show()
- 显示每年总用户数、沉默用户数(未写评论)的比例
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 转换日期格式
df['year'] = pd.to_datetime(df['join_date']).dt.year
# 统计每年总用户数和沉默用户数
total_users = df['year'].value_counts().sort_index()
silent_users = df[df['review_count'] == 0]['year'].value_counts().sort_index()
# 计算比例
silent_ratio = silent_users / total_users
# 可视化
plt.plot(silent_ratio.index, silent_ratio.values)
plt.xlabel('Year')
plt.ylabel('Silent User Ratio')
plt.title('Ratio of Silent Users (No Reviews) per Year')
plt.show()
- 统计出每年的新用户数、评论数、精英用户、tip数、打卡数
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 转换日期格式
df['year'] = pd.to_datetime(df['join_date']).dt.year
# 统计每年的新用户数、评论数、精英用户数、tip数、打卡数
new_users = df[df['year'] == 2022]['user_id'].nunique()
review_counts = df.groupby('year')['review_count'].sum()
elite_counts = df[df['elite'] != 'None'].groupby('year')['user_id'].nunique()
tip_counts = df.groupby('year')['tip_count'].sum()
checkin_counts = df.groupby('year')['checkin_count'].sum()
# 结果表格
result_table = pd.DataFrame({'Year': review_counts.index,
'New Users': new_users,
'Review Count': review_counts.values,
'Elite Users': elite_counts.values,
'Tip Count': tip_counts.values,
'Checkin Count': checkin_counts.values})
# 结果折线图
plt.plot(result_table['Year'], result_table['New Users'], label='New Users')
plt.plot(result_table['Year'], result_table['Review Count'], label='Review Count')
plt.plot(result_table['Year'], result_table['Elite Users'], label='Elite Users')
plt.plot(result_table['Year'], result_table['Tip Count'], label='Tip Count')
plt.plot(result_table['Year'], result_table['Checkin Count'], label='Checkin Count')
plt.xlabel('Year')
plt.ylabel('Count')
plt.title('User Metrics per Year')
plt.legend()
plt.show()
# 洞察分析
insights = """
根据结果表格和折线图的分析,可以看出:
- 2022年是新增用户数量最多的一年。
- 用户评论数量从2019年开始逐年增加,2022年达到最高点。
- 精英用户数量逐年增加,2022年达到最高点。
- 用户tip数量从2019年开始逐年增加,2022年达到最高点。
- 用户打卡数量从2019年开始逐年增加,2022年达到最高点。
"""
print(insights)
# 执行时间
execution_time = """
代码执行时间:X秒
"""
print(execution_time)
评论分析:
- 统计每年的评论数
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 转换日期格式
df['year'] = pd.to_datetime(df['review_date']).dt.year
# 统计每年的评论数
review_counts = df['year'].value_counts().sort_index()
# 可视化
plt.plot(review_counts.index, review_counts.values)
plt.xlabel('Year')
plt.ylabel('Review Count')
plt.title('Number of Reviews per Year')
plt.show()
- 统计有用(helpful)、有趣(funny)及酷(cool)的评论及数量
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 统计有用、有趣和酷评论数量
helpful_counts = df['helpful_count'].sum()
funny_counts = df['funny_count'].sum()
cool_counts = df['cool_count'].sum()
# 可视化
labels = ['Helpful', 'Funny', 'Cool']
counts = [helpful_counts, funny_counts, cool_counts]
plt.bar(labels, counts)
plt.xlabel('Type')
plt.ylabel('Count')
plt.title('Number of Helpful, Funny, and Cool Reviews')
plt.show()
- 每年全部评论用户排行榜
import matplotlib.pyplot as plt
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 转换日期格式
df['year'] = pd.to_datetime(df['review_date']).dt.year
# 按照年份和用户分组,统计评论数量
user_review_counts = df.groupby(['year', 'user_id'])['review_id'].count().reset_index()
# 获取每年评论数量最多的用户
top_users = user_review_counts.groupby('year').apply(lambda x: x.nlargest(5, 'review_id')).reset_index(drop=True)
原文地址: https://www.cveoy.top/t/topic/hJCT 著作权归作者所有。请勿转载和采集!