最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python?Pandas中布爾索引的用法詳解

 更新時(shí)間:2022年08月31日 14:09:56   作者:海擁  
布爾索引是一種使用?DataFrame?中數(shù)據(jù)的實(shí)際值的索引。本文將通過一些示例為大家詳細(xì)講講Python中布爾索引的用法,需要的可以參考一下

在布爾索引中,我們將根據(jù) DataFrame 中數(shù)據(jù)的實(shí)際值而不是它們的行/列標(biāo)簽或整數(shù)位置來選擇數(shù)據(jù)子集。在布爾索引中,我們使用布爾向量來過濾數(shù)據(jù)。

布爾索引是一種使用 DataFrame 中數(shù)據(jù)的實(shí)際值的索引。在布爾索引中,我們可以通過四種方式過濾數(shù)據(jù):

  • 使用布爾索引訪問 DataFrame
  • 將布爾掩碼應(yīng)用于數(shù)據(jù)幀
  • 根據(jù)列值屏蔽數(shù)據(jù)
  • 根據(jù)索引值屏蔽數(shù)據(jù)

使用布爾索引訪問 DataFrame

為了訪問具有布爾索引的數(shù)據(jù)幀,我們必須創(chuàng)建一個(gè)數(shù)據(jù)幀,其中數(shù)據(jù)幀的索引包含一個(gè)布爾值,即“真”或“假”。

例子

# importing pandas as pd
import pandas as pd

# 列表字典
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
		'degree': ["MBA", "BCA", "M.Tech", "MBA"],
		'score':[90, 40, 80, 98]}

df = pd.DataFrame(dict, index = [True, False, True, False])

print(df)

輸出: 

現(xiàn)在我們已經(jīng)創(chuàng)建了一個(gè)帶有布爾索引的數(shù)據(jù)框,之后用戶可以在布爾索引的幫助下訪問數(shù)據(jù)框。用戶可以使用 .loc[]、.iloc[]、.ix[] 三個(gè)函數(shù)訪問數(shù)據(jù)幀 

使用.loc[]訪問具有布爾索引的數(shù)據(jù)框

為了使用 .loc[] 訪問具有布爾索引的數(shù)據(jù)幀,我們只需在 .loc[] 函數(shù)中傳遞一個(gè)布爾值(True 或 False)。

# importing pandas as pd
import pandas as pd

# 列表字典
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
		'degree': ["MBA", "BCA", "M.Tech", "MBA"],
		'score':[90, 40, 80, 98]}

# 使用布爾索引創(chuàng)建數(shù)據(jù)框
df = pd.DataFrame(dict, index = [True, False, True, False])

# 使用 .loc[] 函數(shù)訪問數(shù)據(jù)框
print(df.loc[True])

輸出: 

使用.iloc[]訪問具有布爾索引的數(shù)據(jù)框

為了使用 .iloc[] 訪問數(shù)據(jù)幀,我們必須傳遞一個(gè)布爾值(True 或 False),但 iloc[] 函數(shù)只接受整數(shù)作為參數(shù),因此它會(huì)拋出錯(cuò)誤,因此我們只能在我們?cè)L問數(shù)據(jù)幀時(shí)訪問在 iloc[] 函數(shù)中傳遞一個(gè)整數(shù) 

代碼#1:

# importing pandas as pd
import pandas as pd

# 列表字典
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
		'degree': ["MBA", "BCA", "M.Tech", "MBA"],
		'score':[90, 40, 80, 98]}

# 使用布爾索引創(chuàng)建數(shù)據(jù)框
df = pd.DataFrame(dict, index = [True, False, True, False])

# 使用 .iloc[] 函數(shù)訪問數(shù)據(jù)幀
print(df.iloc[True])

輸出:

TypeError

代碼#2:

# importing pandas as pd
import pandas as pd

# 列表字典
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
		'degree': ["MBA", "BCA", "M.Tech", "MBA"],
		'score':[90, 40, 80, 98]}

# 使用布爾索引創(chuàng)建數(shù)據(jù)框
df = pd.DataFrame(dict, index = [True, False, True, False])


# 使用 .iloc[] 函數(shù)訪問數(shù)據(jù)幀
print(df.iloc[1])

輸出:

使用.ix[]訪問具有布爾索引的數(shù)據(jù)框

為了使用 .ix[] 訪問數(shù)據(jù)幀,我們必須將布爾值(True 或 False)和整數(shù)值傳遞給 .ix[] 函數(shù),因?yàn)槲覀冎?.ix[] 函數(shù)是 .loc[] 的混合體和 .iloc[] 函數(shù)。 

代碼#1:

# importing pandas as pd
import pandas as pd

# 列表字典
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
		'degree': ["MBA", "BCA", "M.Tech", "MBA"],
		'score':[90, 40, 80, 98]}

# 使用布爾索引創(chuàng)建數(shù)據(jù)框
df = pd.DataFrame(dict, index = [True, False, True, False])


# 使用 .ix[] 函數(shù)訪問數(shù)據(jù)幀
print(df.ix[True])

輸出: 

代碼#2:

# importing pandas as pd
import pandas as pd

# 列表字典
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
		'degree': ["MBA", "BCA", "M.Tech", "MBA"],
		'score':[90, 40, 80, 98]}

# 使用布爾索引創(chuàng)建數(shù)據(jù)框
df = pd.DataFrame(dict, index = [True, False, True, False])


# 使用 .ix[] 函數(shù)訪問數(shù)據(jù)幀
print(df.ix[1])

輸出: 

將布爾掩碼應(yīng)用于數(shù)據(jù)框

在數(shù)據(jù)框中,我們可以應(yīng)用布爾掩碼。為此,我們可以使用 getitems 或 [] 訪問器。我們可以通過給出與數(shù)據(jù)幀中包含的長度相同的 True 和 False 列表來應(yīng)用布爾掩碼。當(dāng)我們應(yīng)用布爾掩碼時(shí),它將僅打印我們傳遞布爾值 True 的數(shù)據(jù)幀。

代碼#1:

# importing pandas as pd
import pandas as pd

# 列表字典
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
		'degree': ["MBA", "BCA", "M.Tech", "MBA"],
		'score':[90, 40, 80, 98]}

df = pd.DataFrame(dict, index = [0, 1, 2, 3])



print(df[[True, False, True, False]])

輸出: 

代碼#2: 

# importing pandas package
import pandas as pd

# 從csv文件制作數(shù)據(jù)框
data = pd.read_csv("nba1.1.csv")

df = pd.DataFrame(data, index = [0, 1, 2, 3, 4, 5, 6,
								7, 8, 9, 10, 11, 12])


print(df[[True, False, True, False, True,
	False, True, False, True, False,
				True, False, True]])

輸出: 

根據(jù)列值屏蔽數(shù)據(jù)

在數(shù)據(jù)框中,我們可以根據(jù)列值過濾數(shù)據(jù)。為了過濾數(shù)據(jù),我們可以使用不同的運(yùn)算符對(duì)數(shù)據(jù)框應(yīng)用某些條件,例如 ==、>、<、<=、>=。當(dāng)我們將這些運(yùn)算符應(yīng)用于數(shù)據(jù)幀時(shí),它會(huì)產(chǎn)生一系列真假。

代碼#1:

# importing pandas as pd
import pandas as pd

# 列表字典
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
		'degree': ["BCA", "BCA", "M.Tech", "BCA"],
		'score':[90, 40, 80, 98]}

# 創(chuàng)建數(shù)據(jù)框
df = pd.DataFrame(dict)

# 使用比較運(yùn)算符過濾數(shù)據(jù)
print(df['degree'] == 'BCA')

輸出: 

代碼#2:

# importing pandas package
import pandas as pd

# 從csv文件制作數(shù)據(jù)框
data = pd.read_csv("nba.csv", index_col ="Name")

# 使用大于運(yùn)算符過濾數(shù)據(jù)
print(data['Age'] > 25)

輸出: 

根據(jù)索引值屏蔽數(shù)據(jù): 

在數(shù)據(jù)框中,我們可以根據(jù)列值過濾數(shù)據(jù)。為了過濾數(shù)據(jù),我們可以使用 ==、>、< 等不同的運(yùn)算符根據(jù)索引值創(chuàng)建掩碼。

代碼#1:

# importing pandas as pd
import pandas as pd

# 列表字典
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
		'degree': ["BCA", "BCA", "M.Tech", "BCA"],
		'score':[90, 40, 80, 98]}


df = pd.DataFrame(dict, index = [0, 1, 2, 3])

mask = df.index == 0

print(df[mask])

輸出:

代碼#2:

# importing pandas package
import pandas as pd

# 從csv文件制作數(shù)據(jù)框
data = pd.read_csv("nba1.1.csv")

# 為數(shù)據(jù)框提供索引
df = pd.DataFrame(data, index = [0, 1, 2, 3, 4, 5, 6,
								7, 8, 9, 10, 11, 12])

# 根據(jù)索引值過濾數(shù)據(jù)
mask = df.index > 7

print(df[mask])

輸出: 

以上就是Python Pandas中布爾索引的用法詳解的詳細(xì)內(nèi)容,更多關(guān)于Python Pandas布爾索引的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python3實(shí)現(xiàn)的字典遍歷操作詳解

    Python3實(shí)現(xiàn)的字典遍歷操作詳解

    這篇文章主要介紹了Python3實(shí)現(xiàn)的字典遍歷操作,結(jié)合實(shí)例形式分析了Python3針對(duì)字典鍵、鍵值及鍵值對(duì)遍歷的相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • python中的bisect模塊與二分查找詳情

    python中的bisect模塊與二分查找詳情

    這篇文章主要介紹了python中的bisect模塊與二分查找詳情,bisect是python的內(nèi)置模塊,?用于有序序列的插入和查找。?插入的數(shù)據(jù)不會(huì)影響列表的排序,更多詳細(xì)內(nèi)容需要的朋友可以參考一下
    2022-09-09
  • Python讀取鍵盤輸入的2種方法

    Python讀取鍵盤輸入的2種方法

    這篇文章主要介紹了Python讀取鍵盤輸入的2種方法,主要使用的就是raw_input函數(shù)和input函數(shù),本文分別給出使用實(shí)例,需要的朋友可以參考下
    2015-06-06
  • 使用Mac時(shí)psycopg2導(dǎo)入PyCharm失敗的解決

    使用Mac時(shí)psycopg2導(dǎo)入PyCharm失敗的解決

    這篇文章主要介紹了使用Mac時(shí)psycopg2導(dǎo)入PyCharm失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Python 實(shí)現(xiàn)順序高斯消元法示例

    Python 實(shí)現(xiàn)順序高斯消元法示例

    今天小編就為大家分享一篇Python 實(shí)現(xiàn)順序高斯消元法示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 在Python的Django框架中更新數(shù)據(jù)庫數(shù)據(jù)的方法

    在Python的Django框架中更新數(shù)據(jù)庫數(shù)據(jù)的方法

    這篇文章主要介紹了在Python的Django框架中更新數(shù)據(jù)庫數(shù)據(jù),對(duì)此Django框架中提供了便利的插入和更新方法,需要的朋友可以參考下
    2015-07-07
  • 詳解BeautifulSoup獲取特定標(biāo)簽下內(nèi)容的方法

    詳解BeautifulSoup獲取特定標(biāo)簽下內(nèi)容的方法

    這篇文章主要介紹了詳解BeautifulSoup獲取特定標(biāo)簽下內(nèi)容的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 使用OpenCV對(duì)運(yùn)動(dòng)員的姿勢進(jìn)行檢測功能實(shí)現(xiàn)

    使用OpenCV對(duì)運(yùn)動(dòng)員的姿勢進(jìn)行檢測功能實(shí)現(xiàn)

    2022年奧林匹克運(yùn)動(dòng)會(huì)如期舉行,以不正確的方式進(jìn)行運(yùn)動(dòng)風(fēng)險(xiǎn)在增加,人體姿勢估計(jì)是計(jì)算機(jī)視覺領(lǐng)域的重要問題,接下來通過本文給大家介紹下使用OpenCV對(duì)運(yùn)動(dòng)員的姿勢進(jìn)行檢測功能,感興趣的朋友一起看看吧
    2022-02-02
  • Python實(shí)用技巧之臨時(shí)文件的妙用

    Python實(shí)用技巧之臨時(shí)文件的妙用

    當(dāng)我們用Python編寫程序時(shí),有時(shí)候需要臨時(shí)存儲(chǔ)數(shù)據(jù)且不希望占用多少內(nèi)存,這些情況下以創(chuàng)建臨時(shí)文件的方式進(jìn)行處理,既不會(huì)干擾本地文件系統(tǒng),又安全省事。本文主要介紹了臨時(shí)文件的一些妙用,希望大家能夠喜歡
    2023-02-02
  • python實(shí)現(xiàn)靜態(tài)web服務(wù)器

    python實(shí)現(xiàn)靜態(tài)web服務(wù)器

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)靜態(tài)web服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09

最新評(píng)論

辉南县| 北京市| 华坪县| 迭部县| 保康县| 望谟县| 河津市| 舟山市| 武山县| 玉门市| 榆林市| 阿克| 赞皇县| 吉安市| 页游| 汉阴县| 龙口市| 炉霍县| 随州市| 富平县| 稷山县| 邯郸市| 和林格尔县| 抚松县| 罗山县| 资阳市| 沁水县| 昌黎县| 沙湾县| 林口县| 黑河市| 安多县| 扬中市| 峨边| 双辽市| 闻喜县| 禄丰县| 德昌县| 龙州县| 怀化市| 财经|