You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何统计Weather列不同天气类型数量并展示TOP10?代码正确性咨询

问题分析

你当前写的weather=df.head(10)完全不符合需求,这个代码的作用是提取数据集的前10行记录,根本没有对Weather列的类别进行统计和排名。

正确实现方案

要完成统计Weather列不同天气标识的数量并展示排名前10的类别,直接用Pandas的内置方法就能高效实现:

1. 统计各天气类别的出现频次

使用value_counts()方法可以直接统计列中每个类别的出现次数,结果会自动按频次从高到低排序:

weather_counts = df['Weather'].value_counts()

2. 提取排名前10的类别

基于统计结果,直接取前10项即可:

top10_weather = weather_counts.head(10)

3. 输出结果

打印结果查看:

print("排名前10的天气类别及数量:")
print(top10_weather)

如果需要更直观的可视化展示(需提前安装matplotlib),可以添加以下代码:

import matplotlib.pyplot as plt

top10_weather.plot(kind='bar', figsize=(10,6), title='Top 10 Weather Categories')
plt.xlabel('Weather Type')
plt.ylabel('Occurrence Count')
plt.xticks(rotation=45)  # 旋转x轴标签避免重叠
plt.tight_layout()
plt.show()

完整示例代码

import pandas as pd
import matplotlib.pyplot as plt

# 假设已通过pd.read_csv将数据集导入到df变量中
df = pd.read_csv('your_dataset.csv')

# 统计并提取前10天气类别
weather_counts = df['Weather'].value_counts()
top10_weather = weather_counts.head(10)

# 打印结果
print(top10_weather)

# 可选:可视化展示
top10_weather.plot(kind='bar', figsize=(10,6), title='Top 10 Weather Categories')
plt.xlabel('Weather Type')
plt.ylabel('Occurrence Count')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

内容的提问来源于stack exchange,提问作者SoaR_vX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 16:51:37