欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python pandas dataframe如何獲取除了指定列以外的所有列

 更新時(shí)間:2023年09月12日 11:10:18   作者:宇宙全能王  
這篇文章主要介紹了python pandas dataframe如何獲取除了指定列以外的所有列問題,具有很好的參考價(jià)值,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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
Namecourse1course2course3fruitsport
1Anna859082applebasketball
2Betty838586bananavolleyball
3Richard908381applefootball
4Philip848891orangebasketball
5Paul858485peachbaseball

方法一

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']]
course2fruit
190apple
285banana
383apple
488orange
584peach

或者以 column list (list 變量)的形式導(dǎo)入到 df[ ] 中,

例如:

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

可以用 column list=df.columns[start:end] 的方式選擇連續(xù)列,start 和 end 均為數(shù)字,不包括 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'] 來索引連續(xù)列,就會(huì)報(bào)錯(cuò)。而輸入數(shù)字索引 df[1:3] 時(shí),結(jié)果不再是列索引,而是行索引,

如下所示:

df[1:3]
Namecourse1course2course3fruitsport
2Betty838586bananavolleyball
3Richard908381applefootball

以下兩種方法 df.loc[]和df.iloc[]就可以解決這個(gè)問題,可以明確行或列索引。還可以同時(shí)取多行和多列。

方法二

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

輸入 column_list 選擇多列 [:, column_list] ,括號(hào)中第一個(gè) : 表示選擇全部行。

例如:

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

選擇連續(xù)多列 [:,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[ ]括號(hào)內(nèi)也可以輸入判斷語句,結(jié)果是對(duì)行做篩選。

例如:

df.loc[df['course1']>84]
#注:輸入df[df['course1']>84],輸出結(jié)果相同
Namecourse1course2course3fruitsport
1Anna859082applebasketball
3Richard908381applefootball
5Paul858485peachbaseball

方法三

df.iloc[]: i 表示 integer,用 integer location(行或列的整數(shù)位置,從0開始)做索引。

df.iloc與df.loc用法類似,只是索引項(xiàng)不同。

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

選擇連續(xù)多列: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[] 括號(hào)內(nèi)不可以輸入判斷語句。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論