尔诈我虞网

pandas中如何提取DataFrame的某些列

pandas中如何提取DataFrame的某些列

在处理表格型数据时,中何一行数据是某列一个 sample,列就是中何待提取的特征。怎么选取其中的某列一些列呢?本文分享一些方法。

使用如下的中何数据作为例子:

import pandas as pd
data = pd.DataFrame({ 'Name':['Anna', 'Betty', 'Richard', 'Philip','Paul'],        'course1':[85,83,90,84,85],        'course2':[90,85,83,88,84],        'course3':[82,86,81,91,85],        'fruit':['apple','banana','apple','orange','peach'],        'sport':['basketball', 'volleyball', 'football', 'basketball','baseball']},         index=[1,2,3,4,5])  df = pd.DataFrame(data)
df
Namecourse1course2course3fruitsport
1Anna859082applebasketball
2Betty838586bananavolleyball
3Richard908381applefootball
4Philip848891orangebasketball
5Paul858485peachbaseball

方法一:df[columns]

先看最简单的情况。输入列名,某列选择一列。中何例如:

df['course2']
1    902    853    834    885    84Name: course2,某列 dtype: int64

df[column list]:选择列。例如:

df[['course2',中何'fruit']]
course2fruit
190apple
285banana
383apple
488orange
584peach

或者以 column list (list 变量)的形式导入到 df[ ] 中,例如:

select_cols=['course2',某列'fruit']df[select_cols]
course2fruit
190apple
285banana
383apple
488orange
584peach

可以用 column list=df.columns[start:end] 的方式选择连续列,start 和 end 均为数字,中何不包括 end 列。某列例如:

select_cols=df.columns[1:4]df[select_cols]
course1course2course3
1859082
2838586
3908381
4848891
5858485

你可能注意到,中何其中有 3 列的某列名字相近:‘course1’,‘course2’,‘course3’。怎么提取这三列呢?这里分享在Kaggle 上看到 一位大神使用的中何 list comprehension方法。

select_cols=[c for c in df.columns if 'course' in c]df[select_cols]
course1course2course3
1859082
2838586
3908381
4848891
5858485

但是,如果你想输入df['course1':'course3']来索引连续列,就会报错。而输入数字索引df[1:3]时,结果不再是列索引,而是行索引,如下所示:

df[1:3]
Namecourse1course2course3fruitsport
2Betty838586bananavolleyball
3Richard908381applefootball

以下两种方法 df.loc[]和df.iloc[]就可以解决这个问题,可以明确行或列索引。还可以同时取多行和多列。

方法二:df.loc[]:用 label (行名或列名)做索引。

输入 column_list 选择多列 [:, column_list],括号中第一个:表示选择全部行。例如:

df.loc[:,['course2','fruit']]
course2fruit
190apple
285banana
383apple
488orange
584peach

选择连续多列 [:,start_col: end_col],注意:包括 end_col。例如:

df.loc[:,'course2':'fruit']
course2course3fruit
19082apple
28586banana
38381apple
48891orange
58485peach

选择多行和多列,例如:

df.loc[1:3,'course2':'fruit']
course2course3fruit
19082apple
28586banana
38381apple

与 df[ ]类似,df.loc[ ]括号内也可以输入判断语句,结果是对行做筛选。例如:

df.loc[df['course1']>84]#注:输入df[df['course1']>84],输出结果相同
Namecourse1course2course3fruitsport
1Anna859082applebasketball
3Richard908381applefootball
5Paul858485peachbaseball

方法三:df.iloc[]: i 表示 integer,用 integer location(行或列的整数位置,从0开始)做索引。

df.iloc与df.loc用法类似,只是索引项不同。

df.iloc[:,[2,4]]
course2fruit
190apple
285banana
383apple
488orange
584peach

选择连续多列:df.iloc[:, start_ix:end_ix],注意:不包括 end_ix。例如:

df.iloc[:,2:5]
course2course3fruit
19082apple
28586banana
38381apple
48891orange
58485peach

选择多行与多列,例如:

df.iloc[1:3,[2,4]]
course2fruit
285banana
383apple

与 df.loc[] 不同,df.iloc[] 括号内不可以输入判断语句。

觉得本文不错的话,请点赞支持一下吧,谢谢!

关注我 宁萌Julie,互相学习,多多交流呀!

参考:

1.如何选取dataframe的多列-教程:https://www.geeksforgeeks.org/how-to-select-multiple-columns-in-a-pandas-dataframe/

2.用 list comprehension 选择多列:https://www.kaggle.com/code/robikscube/ieee-fraud-detection-first-look-and-eda/notebook

3.df.loc 与 df.iloc 的比较:https://stackoverflow.com/questions/31593201/how-are-iloc-and-loc-different

未经允许不得转载:尔诈我虞网 » pandas中如何提取DataFrame的某些列