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

如何按列值筛选图表?DataFrame指定年份范围绘图求助

解决方法

步骤1:筛选目标数据

通过布尔索引过滤出Period大于2018的行,生成新的DataFrame:

# 筛选Period > 2018的数据
filtered_df = df[df['Period'] > 2018]

步骤2:用筛选后的数据绘图

直接使用筛选后的filtered_df调用绘图方法,逻辑和你原本的代码一致:

filtered_df.plot(x="Period", y="Quantity", kind="line")

特殊情况处理

如果你的Period列是字符串格式(比如"2019"),需要先转换成数值类型再筛选:

# 转换Period列为整数类型
df['Period'] = df['Period'].astype(int)
# 重复筛选和绘图步骤
filtered_df = df[df['Period'] > 2018]
filtered_df.plot(x="Period", y="Quantity", kind="line")

内容的提问来源于stack exchange,提问作者Victor viñas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 12:27:02