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| Anna | 85 | 90 | 82 | apple | basketball | 
| Betty | 83 | 85 | 86 | banana | volleyball | 
| Richard | 90 | 83 | 81 | apple | football | 
| Philip | 84 | 88 | 91 | orange | basketball | 
| Paul | 85 | 84 | 85 | peach | baseball | 
方法一:df[columns]
先看最簡單的情況。輸入列名,選擇一列。例如:
df['course2'] 1 90 2 85 3 83 4 88 5 84 Name: course2, dtype: int64df[column list]:選擇列。例如:
df[['course2','fruit']]| 90 | apple | 
| 85 | banana | 
| 83 | apple | 
| 88 | orange | 
| 84 | peach | 
或者以 column list (list 變量)的形式導入到 df[ ] 中,例如:
select_cols=['course2','fruit'] df[select_cols]| 90 | apple | 
| 85 | banana | 
| 83 | apple | 
| 88 | orange | 
| 84 | peach | 
可以用 column list=df.columns[start:end] 的方式選擇連續列,start 和 end 均為數字,不包括 end 列。例如:
select_cols=df.columns[1:4] df[select_cols]| 85 | 90 | 82 | 
| 83 | 85 | 86 | 
| 90 | 83 | 81 | 
| 84 | 88 | 91 | 
| 85 | 84 | 85 | 
你可能注意到,其中有 3 列的名字相近:‘course1’,‘course2’,‘course3’。怎么提取這三列呢?這里分享在Kaggle 上看到 一位大神使用的 list comprehension方法。
select_cols=[c for c in df.columns if 'course' in c] df[select_cols]| 85 | 90 | 82 | 
| 83 | 85 | 86 | 
| 90 | 83 | 81 | 
| 84 | 88 | 91 | 
| 85 | 84 | 85 | 
但是,如果你想輸入df['course1':'course3'] 來索引連續列,就會報錯。而輸入數字索引df[1:3]時,結果不再是列索引,而是行索引,如下所示:
df[1:3]| Betty | 83 | 85 | 86 | banana | volleyball | 
| Richard | 90 | 83 | 81 | apple | football | 
以下兩種方法 df.loc[]和df.iloc[]就可以解決這個問題,可以明確行或列索引。還可以同時取多行和多列。
方法二:df.loc[]:用 label (行名或列名)做索引。
輸入 column_list 選擇多列 [:, column_list],括號中第一個: 表示選擇全部行。例如:
df.loc[:,['course2','fruit']]| 90 | apple | 
| 85 | banana | 
| 83 | apple | 
| 88 | orange | 
| 84 | peach | 
選擇連續多列 [:,start_col: end_col],注意:包括 end_col。例如:
df.loc[:,'course2':'fruit']| 90 | 82 | apple | 
| 85 | 86 | banana | 
| 83 | 81 | apple | 
| 88 | 91 | orange | 
| 84 | 85 | peach | 
選擇多行和多列,例如:
df.loc[1:3,'course2':'fruit']| 90 | 82 | apple | 
| 85 | 86 | banana | 
| 83 | 81 | apple | 
與 df[ ]類似,df.loc[ ]括號內也可以輸入判斷語句,結果是對行做篩選。例如:
df.loc[df['course1']>84] #注:輸入df[df['course1']>84],輸出結果相同| Anna | 85 | 90 | 82 | apple | basketball | 
| Richard | 90 | 83 | 81 | apple | football | 
| Paul | 85 | 84 | 85 | peach | baseball | 
方法三:df.iloc[]: i 表示 integer,用 integer location(行或列的整數位置,從0開始)做索引。
df.iloc與df.loc用法類似,只是索引項不同。
df.iloc[:,[2,4]]| 90 | apple | 
| 85 | banana | 
| 83 | apple | 
| 88 | orange | 
| 84 | peach | 
選擇連續多列:df.iloc[:, start_ix:end_ix],注意:不包括 end_ix。例如:
df.iloc[:,2:5]| 90 | 82 | apple | 
| 85 | 86 | banana | 
| 83 | 81 | apple | 
| 88 | 91 | orange | 
| 84 | 85 | peach | 
選擇多行與多列,例如:
df.iloc[1:3,[2,4]]| 85 | banana | 
| 83 | apple | 
與 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的某些列的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 公务员面试:面谈
- 下一篇: 阿里云ACP云计算错题集41-70
