程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How does the python pandas dataframe return values according to specific conditions?

編輯:Python

I have a problem , I have one pandas Of dataframe:
book_ratings

I want to base it on User 1 To User 4 The score , Find out if the score reaches 5 The name of the book divided

I tried to use book_ratings.isin([5]), We can only find True and False Value , How to return the corresponding value ToT




Take the answer :

You can do this :

import pandas as pdpd.set_option('display.max_columns',None)df=pd.DataFrame({'title':['a','b','c','d'],'author':['aa','bb','cc','dd'],'user1':[3.2,2.9,2.5,2.9],'user2':[5.0,1.3,4.0,3.8],'user3':[2.0,2.3,2.8,4.0],'user4':[4.0,3.5,4.0,5.0]})a=df.iloc[:,2:].isin([5]).any(axis=1)df=df.mask(~a).dropna()print(df)

result:

title author user1 user2 user3 user40 a aa 3.2 5.0 2.0 4.03 d dd 2.9 3.8 4.0 5.0


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved