以手机评论数据为例,该文件的数据列包括手机品牌、价格和评分,请完成下列数据分析 任务。 按手机价格统计评分最大值、最小值、算术平均值和中位数。 按手机品牌统计评分最大值、最小值、算术平均值和中位数。
时间: 2024-03-16 15:47:56 浏览: 17
假设你已经将手机评论数据存储在名为 `data` 的列表中,每条评论数据是一个字典,包含三个键值对:`brand` 表示手机品牌,`price` 表示手机价格,`score` 表示评分。以下是代码实现:
按手机价格统计评分:
```python
# 首先将数据按价格从小到大排序
sorted_data = sorted(data, key=lambda x: x['price'])
# 定义一个字典用于存储统计结果
price_scores = {}
# 遍历数据,按价格统计评分
for d in sorted_data:
price = d['price']
score = d['score']
if price not in price_scores:
price_scores[price] = [score]
else:
price_scores[price].append(score)
# 遍历统计结果,计算最大值、最小值、算术平均值和中位数
for price in price_scores:
scores = price_scores[price]
max_score = max(scores)
min_score = min(scores)
avg_score = sum(scores) / len(scores)
n = len(scores)
median_score = (scores[n // 2] + scores[(n - 1) // 2]) / 2 if n % 2 == 0 else scores[n // 2]
print(f"Price: {price}, Max Score: {max_score}, Min Score: {min_score}, Avg Score: {avg_score}, Median Score: {median_score}")
```
按手机品牌统计评分:
```python
# 定义一个字典用于存储统计结果
brand_scores = {}
# 遍历数据,按品牌统计评分
for d in data:
brand = d['brand']
score = d['score']
if brand not in brand_scores:
brand_scores[brand] = [score]
else:
brand_scores[brand].append(score)
# 遍历统计结果,计算最大值、最小值、算术平均值和中位数
for brand in brand_scores:
scores = brand_scores[brand]
max_score = max(scores)
min_score = min(scores)
avg_score = sum(scores) / len(scores)
n = len(scores)
median_score = (scores[n // 2] + scores[(n - 1) // 2]) / 2 if n % 2 == 0 else scores[n // 2]
print(f"Brand: {brand}, Max Score: {max_score}, Min Score: {min_score}, Avg Score: {avg_score}, Median Score: {median_score}")
```