python pandas dataframe如何獲取除了指定列以外的所有列
pandas dataframe獲取除了指定列以外的所有列
假設(shè)有個(gè)dataframe叫做df,有[‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘g’]列,但是你只想要除了’e’列之外的所有列
兩種方法
方法一
del df['e']
方法二
col = df.columns.values.tolist() col.remove('e') ?# 切不可用col = col.remove('e'),或者直接把remove加到上面這行代碼中,否則col為空 new_df = df[col].copy() print(new_df)
pandas中提取DataFrame的某些列
在處理表格型數(shù)據(jù)時(shí),一行數(shù)據(jù)是一個(gè) sample,列就是待提取的特征。怎么選取其中的一些列呢?本文分享一些方法。
使用如下的數(shù)據(jù)作為例子:
import pandas as pd
df = 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
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]
先看最簡(jiǎn)單的情況。輸入列名,選擇一列。
例如:
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 變量)的形式導(dǎo)入到 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] 的方式選擇連續(xù)列,start 和 end 均為數(shù)字,不包括 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']
來索引連續(xù)列,就會(huì)報(bào)錯(cuò)。而輸入數(shù)字索引 df[1:3]
時(shí),結(jié)果不再是列索引,而是行索引,
如下所示:
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[]就可以解決這個(gè)問題,可以明確行或列索引。還可以同時(shí)取多行和多列。
方法二
df.loc[]:用 label (行名或列名)做索引。
輸入 column_list 選擇多列 [:, column_list]
,括號(hào)中第一個(gè) :
表示選擇全部行。
例如:
df.loc[:,['course2','fruit']]
course2 | fruit | |
---|---|---|
1 | 90 | apple |
2 | 85 | banana |
3 | 83 | apple |
4 | 88 | orange |
5 | 84 | peach |
選擇連續(xù)多列 [:,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[ ]括號(hào)內(nèi)也可以輸入判斷語句,結(jié)果是對(duì)行做篩選。
例如:
df.loc[df['course1']>84] #注:輸入df[df['course1']>84],輸出結(jié)果相同
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(行或列的整數(shù)位置,從0開始)做索引。
df.iloc與df.loc用法類似,只是索引項(xiàng)不同。
df.iloc[:,[2,4]]
course2 | fruit | |
---|---|---|
1 | 90 | apple |
2 | 85 | banana |
3 | 83 | apple |
4 | 88 | orange |
5 | 84 | peach |
選擇連續(xù)多列: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[] 括號(hào)內(nèi)不可以輸入判斷語句。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Pytorch使用DataLoader實(shí)現(xiàn)批量加載數(shù)據(jù)
這篇文章主要介紹了Pytorch使用DataLoader實(shí)現(xiàn)批量加載數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02python數(shù)組復(fù)制拷貝的實(shí)現(xiàn)方法
這篇文章主要介紹了python數(shù)組復(fù)制拷貝的實(shí)現(xiàn)方法,實(shí)例分析了Python數(shù)組傳地址與傳值兩種復(fù)制拷貝的使用技巧,需要的朋友可以參考下2015-06-06Python+OpenCV實(shí)現(xiàn)在圖像上繪制矩形
這篇文章主要介紹了如何利用Python和OpenCV實(shí)現(xiàn)在圖像上繪制任意大小的矩形,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2022-03-03Python實(shí)現(xiàn)批量將PPT轉(zhuǎn)換成長(zhǎng)圖
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)批量將PPT轉(zhuǎn)換成長(zhǎng)圖,并且圖片名稱與PPT文件名稱相同,保存位置相同,感興趣的小伙伴可以了解下2023-08-08python實(shí)現(xiàn)每天自動(dòng)簽到領(lǐng)積分的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)每天自動(dòng)簽到領(lǐng)積分的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Python實(shí)現(xiàn)的爬取百度貼吧圖片功能完整示例
這篇文章主要介紹了Python實(shí)現(xiàn)的爬取百度貼吧圖片功能,結(jié)合完整實(shí)例形式分析了Python實(shí)現(xiàn)的百度貼吧圖片爬蟲相關(guān)操作技巧,需要的朋友可以參考下2019-05-05python中邏輯與或(and、or)和按位與或異或(&、|、^)區(qū)別
這篇文章主要介紹了python中邏輯與或(and、or)和按位與或異或(&、|、^)區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08使用Python OpenCV為CNN增加圖像樣本的實(shí)現(xiàn)
這篇文章主要介紹了使用Python OpenCV為CNN增加圖像樣本的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06在Python中處理日期和時(shí)間的基本知識(shí)點(diǎn)整理匯總
這篇文章主要介紹了在Python中處理日期和時(shí)間的基本知識(shí)點(diǎn)整理匯總,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05