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

Python中pandas的dataframe過濾數(shù)據(jù)方法

 更新時間:2023年07月06日 09:08:19   作者:菜鳥小超  
這篇文章主要介紹了Python中pandas的dataframe過濾數(shù)據(jù)方法,Pandas是另外一個用于處理高級數(shù)據(jù)結構和數(shù)據(jù)分析的Python庫,Pandas是基于Numpy構建的一種工具,需要的朋友可以參考下

  • 選取某一列中大于某個值的行:
df[df['column_name'] > value]
  • 選取某一列中滿足多個條件的行:
df[(df['column_name'] > value1) & (df['column_name'] < value2)]
  • 選取某一列中不等于某個值的行:
df[df['column_name'] != value]
  • 選取某一列中包含某個字符串的行:
df[df['column_name'].str.contains('string')]
  • 選取多個列中滿足某個條件的行:
df[(df['column_name1'] > value1) & (df['column_name2'] < value2)]

在以上例子中,df是pandas DataFrame對象,‘column_name’、‘column_name1’、'column_name2’是DataFrame的列名,value、value1、value2是篩選條件。通過這些布爾索引的使用,可以方便地選取滿足特定條件的行。

演示demo

假設有一個包含電影信息的數(shù)據(jù)集,包含電影名稱、導演、評分等信息。數(shù)據(jù)集如下:

電影名稱導演評分
TitanicJames Cameron7.8
AvatarJames Cameron8.5
The Shawshank RedemptionFrank Darabont9.3
The GodfatherFrancis Ford Coppola9.2
The Dark KnightChristopher Nolan9.0
InceptionChristopher Nolan8.8

下面是使用布爾索引過濾數(shù)據(jù)的例子:

輸出結果如下:

             電影名稱                    導演   評分
0         Titanic          James Cameron  7.8
1          Avatar          James Cameron  8.5
2  The Shawshank Redemption    Frank Darabont  9.3
3    The Godfather  Francis Ford Coppola  9.2
4  The Dark Knight     Christopher Nolan  9.0
5        Inception     Christopher Nolan  8.8

              電影名稱                 導演   評分
4   The Dark Knight  Christopher Nolan  9.0
5         Inception  Christopher Nolan  8.8

                   電影名稱                 導演   評分
2  The Shawshank Redemption    Frank Darabont  9.3

在以上例子中,第一個過濾條件是選取評分大于8.0的電影,第二個過濾條件是選取由Christopher Nolan執(zhí)導的電影,第三個過濾條件是選取電影名稱中包含"Redemption"的電影。根據(jù)這些過濾條件,我們可以得到不同的結果。

到此這篇關于Python中pandas的dataframe過濾數(shù)據(jù)方法的文章就介紹到這了,更多相關pandas的dataframe過濾數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論