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

如何在R中筛选表格特定行,按燃油类型拆分得到独立dataframe

按燃油类型拆分Cars数据集为独立DataFrame的方案

前提说明

现有cars数据集包含Fuel.type字段,取值为Gas和Diesel两类,需要拆分得到两个保留所有字段的独立DataFrame。

Python Pandas 实现代码

  • 导入依赖并读入数据集
import pandas as pd
# 替换为你的数据集实际路径
cars_df = pd.read_csv("cars.csv")
  • 拆分得到两类数据
# 仅保留Gas类型的全列数据
gas_df = cars_df[cars_df["Fuel.type"] == "Gas"].copy()
# 仅保留Diesel类型的全列数据
diesel_df = cars_df[cars_df["Fuel.type"] == "Diesel"].copy()

末尾加.copy()是为了避免后续修改新DataFrame时触发Pandas的链式索引警告。

输出结果示例

Gas类型数据框

ModelMakeMfg.YearFuel.typeMiles
CivicHonda2020Gas1000
CTSCadillac2018Gas5000

Diesel类型数据框

ModelMakeMfg.YearFuel.typeMiles
SonataHyundai2017Diesel4500
FusionFord2016Diesel6000

R语言实现代码

# 读入数据集
cars_df <- read.csv("cars.csv")
# 拆分Gas类型数据框
gas_df <- subset(cars_df, Fuel.type == "Gas")
# 拆分Diesel类型数据框
diesel_df <- subset(cars_df, Fuel.type == "Diesel")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 13:51:02