網站首頁 編程語言 正文
前言
在處理表格型數據時,一行數據是一個 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
? | Name | course1 | course2 | course3 | fruit | sport |
---|---|---|---|---|---|---|
1 | Anna | 85 | 90 | 82 | apple | basketball |
2 | Betty | 83 | 85 | 86 | banana | volleyball |
3 | Richard | 90 | 83 | 81 | apple | football |
4 | Philip | 84 | 88 | 91 | orange | basketball |
5 | Paul | 85 | 84 | 85 | peach | baseball |
方法一:df[columns]
先看最簡單的情況。輸入列名,選擇一列。例如:
df['course2']
1 90
2 85
3 83
4 88
5 84
Name: course2, dtype: int64
df[column list]:選擇列。例如:
df[['course2','fruit']]
? | course2 | fruit |
---|---|---|
1 | 90 | apple |
2 | 85 | banana |
3 | 83 | apple |
4 | 88 | orange |
5 | 84 | peach |
或者以 column list (list 變量)的形式導入到 df[ ] 中,例如:
select_cols=['course2','fruit']
df[select_cols]
? | course2 | fruit |
---|---|---|
1 | 90 | apple |
2 | 85 | banana |
3 | 83 | apple |
4 | 88 | orange |
5 | 84 | peach |
可以用 column list=df.columns[start:end] 的方式選擇連續列,start 和 end 均為數字,不包括 end 列。例如:
select_cols=df.columns[1:4]
df[select_cols]
? | course1 | course2 | course3 |
---|---|---|---|
1 | 85 | 90 | 82 |
2 | 83 | 85 | 86 |
3 | 90 | 83 | 81 |
4 | 84 | 88 | 91 |
5 | 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]
? | course1 | course2 | course3 |
---|---|---|---|
1 | 85 | 90 | 82 |
2 | 83 | 85 | 86 |
3 | 90 | 83 | 81 |
4 | 84 | 88 | 91 |
5 | 85 | 84 | 85 |
但是,如果你想輸入df['course1':'course3'] 來索引連續列,就會報錯。而輸入數字索引df[1:3]時,結果不再是列索引,而是行索引,如下所示:
df[1:3]
? | Name | course1 | course2 | course3 | fruit | sport |
---|---|---|---|---|---|---|
2 | Betty | 83 | 85 | 86 | banana | volleyball |
3 | Richard | 90 | 83 | 81 | apple | football |
以下兩種方法 df.loc[]和df.iloc[]就可以解決這個問題,可以明確行或列索引。還可以同時取多行和多列。
方法二:df.loc[]:用 label (行名或列名)做索引。
輸入 column_list 選擇多列 [:, column_list],括號中第一個: 表示選擇全部行。例如:
df.loc[:,['course2','fruit']]
? | course2 | fruit |
---|---|---|
1 | 90 | apple |
2 | 85 | banana |
3 | 83 | apple |
4 | 88 | orange |
5 | 84 | peach |
選擇連續多列 [:,start_col: end_col],注意:包括 end_col。例如:
df.loc[:,'course2':'fruit']
? | course2 | course3 | fruit |
---|---|---|---|
1 | 90 | 82 | apple |
2 | 85 | 86 | banana |
3 | 83 | 81 | apple |
4 | 88 | 91 | orange |
5 | 84 | 85 | peach |
選擇多行和多列,例如:
df.loc[1:3,'course2':'fruit']
? | course2 | course3 | fruit |
---|---|---|---|
1 | 90 | 82 | apple |
2 | 85 | 86 | banana |
3 | 83 | 81 | apple |
與 df[ ]類似,df.loc[ ]括號內也可以輸入判斷語句,結果是對行做篩選。例如:
df.loc[df['course1']>84]
#注:輸入df[df['course1']>84],輸出結果相同
? | Name | course1 | course2 | course3 | fruit | sport |
---|---|---|---|---|---|---|
1 | Anna | 85 | 90 | 82 | apple | basketball |
3 | Richard | 90 | 83 | 81 | apple | football |
5 | Paul | 85 | 84 | 85 | peach | baseball |
方法三:df.iloc[]: i 表示 integer,用 integer location(行或列的整數位置,從0開始)做索引。
df.iloc與df.loc用法類似,只是索引項不同。
df.iloc[:,[2,4]]
? | course2 | fruit |
---|---|---|
1 | 90 | apple |
2 | 85 | banana |
3 | 83 | apple |
4 | 88 | orange |
5 | 84 | peach |
選擇連續多列:df.iloc[:, start_ix:end_ix],注意:不包括 end_ix。例如:
df.iloc[:,2:5]
? | course2 | course3 | fruit |
---|---|---|---|
1 | 90 | 82 | apple |
2 | 85 | 86 | banana |
3 | 83 | 81 | apple |
4 | 88 | 91 | orange |
5 | 84 | 85 | peach |
選擇多行與多列,例如:
df.iloc[1:3,[2,4]]
? | course2 | fruit |
---|---|---|
2 | 85 | banana |
3 | 83 | apple |
與 df.loc[] 不同,df.iloc[] 括號內不可以輸入判斷語句。
補充:提取所有列名中包含“線索”、“瀏覽”字段的列
import pandas as pd
path = 'F:\python_projects\python_learning\ershouche.csv'
df = pd.read_csv(open(path), index_col=0)
df = df.fillna(0) # 填充空值后需賦值
print(df.describe())
columns = df.columns.values.tolist() # 獲取列名列表,注意values,tolist的使用
col_xian = [] # 存儲包含‘線索'字段的列名
for i in columns:
if '線索' in i:
col_xian.append(i)
col_liu = [] # 存儲包含‘瀏覽'字段的列名
for i in columns:
if '瀏覽' in i:
col_liu.append(i)
df_xian = df[col_xian] # 根據列名取列
df_liu = df[col_liu]
參考:
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
總結
原文鏈接:https://blog.csdn.net/applebear1123/article/details/125128742
相關推薦
- 2023-01-27 Redux?Toolkit的基本使用示例詳解(Redux工具包)_React
- 2022-08-27 C#從前面或后面按指定數量刪除字符串_C#教程
- 2022-09-25 C++ sort比較函數的寫法,最全面的總結
- 2022-03-19 淺談Go1.18中的泛型編程_Golang
- 2022-10-26 Python?模擬死鎖的常見實例詳解_python
- 2022-03-28 深入理解numpy中argmax的具體使用_python
- 2023-10-16 Nginx啟動,重啟以及基本命令
- 2023-05-21 Python使用win32com.client的方法示例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支