有时候,我们仅需要分析数据集的某些部分
今天要为大家介绍如何使用Excel 和 Python进行列数据选择
案例数据继续使用UCI的红酒质量评分数据集
需要完成的任务是选择该数据集的列数据
知识点14:选择某一列
Excel
鼠标单击某列的顶端列编号,即可选择某一列
Python
import pandas as pd #载入pandas库
data = pd.read_excel('C:/Users/dell-pc/Desktop/data.xlsx') #读取Excel文件数据
data.head(5) #显示数据集的前5行
data_column = data["fixed acidity"] #使用列标签选择"fixed acidity"列
data_column.head(5) #显示该列的前5行
data_column = data.iloc[:, 0] #使用列索引选择"fixed acidity"列(列索引从0开始计数)
data_column.head(5) #显示该列的前5行
知识点15:选择连续的某几列
Excel
鼠标单击某列的顶端列编号,向右拖动,即可选择连续的某几列
Python
data_column = data.loc[:, "fixed acidity" : "citric acid"] #使用列标签选择前3列
data_column.head(5) #显示数据集的前5行
data_column = data.iloc[:, 0 : 3] #使用列索引选择前3列(上限不入组)
data_column.head(5) #显示数据集的前5行
知识点16:选择不连续的某几列
Excel
鼠标单击某列的顶端列编号,按住Ctrl键,再单击其他列的顶端列编号,即可选择不连续的某几列
Python
data_column = data.loc[:, ["fixed acidity", "citric acid", "chlorides"]] #使用列标签选择第1、3、5列
data_column.head(5) #显示数据集的前5行
data_column = data.iloc[:, [0, 2, 4]] #使用列索引选择第1、3、5列
data_column.head(5) #显示数据集的前5行