您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么使用Python的Pandas布爾索引”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么使用Python的Pandas布爾索引”吧!
1.計算布爾值統計信息
import pandas as pd import numpy as np import matplotlib.pyplot as plt #讀取movie,設定行索引是movie_title pd.options.display.max_columns = 50 movie = pd.read_csv("./data/movie.csv",index_col = 'movie_title') #判斷電影時長是否超過兩個小時 #Figure1 movie_2_hours = movie['duration'] > 120 #統計時長超過兩小時的電影總數 print(movie_2_hours.sum()) #result:1039 #統計時長超過兩小時的電影的比例 print(movie_2_hours.mean()) #統計False和True的比例 print(movie_2_hours.value_counts(normalize = True)) #比較同一個DataFrame中的兩列 actors = movie[['actor_1_facebook_likes','actor_2_facebook_likes']].dropna() print((actors['actor_1_facebook_likes'] > actors['actor_2_facebook_likes']).mean()) #Figure2
運行結果:
Figure1
Figure2
2. 構建多個布爾條件
import pandas as pd import numpy as np import matplotlib.pyplot as plt #讀取movie,設定行索引是movie_title pd.options.display.max_columns = 50 movie = pd.read_csv("./data/movie.csv",index_col = 'movie_title') #創建多個布爾條件 criteria1 = movie.imdb_score > 8 criteria2 = movie.content_rating == "PG-13" criteria3 = (movie.title_year < 2000) | (movie.title_year >= 2010) """ print(criteria1.head()) print(criteria2.head()) print(criteria3.head()) 運行結果:Figure1 """ #將多個布爾條件合并成一個 criteria_final = criteria1 & criteria2 & criteria3 print(criteria_final.head()) #運行結果:Figure2
運行結果:
Figure1
Figure2
3.用布爾索引過濾
import pandas as pd import numpy as np import matplotlib.pyplot as plt #讀取movie,設定行索引是movie_title pd.options.display.max_columns = 50 movie = pd.read_csv("./data/movie.csv",index_col = 'movie_title') #創建第一個布爾條件 crit_a1 = movie.imdb_score > 8 crit_a2 = movie.content_rating == 'PG-13' crit_a3 = (movie.title_year < 2000) | (movie.title_year > 2009) final_crit_a = crit_a1 & crit_a2 & crit_a3 #創建第二個布爾條件 crit_b1 = movie.imdb_score < 5 crit_b2 = movie.content_rating == 'R' crit_b3 = (movie.title_year >= 2000) & (movie.title_year <= 2010) final_crit_b = crit_b1 & crit_b2 & crit_b3 #將兩個條件用或運算合并起來 final_crit_all = final_crit_a | final_crit_b print(final_crit_all.head()) #Figure 1 #用最終的布爾條件過濾數據 print(movie[final_crit_all].head()) #Figure2
運行結果:
Figure1
Figure2
import pandas as pd import numpy as np import matplotlib.pyplot as plt #讀取movie,設定行索引是movie_title pd.options.display.max_columns = 50 movie = pd.read_csv("./data/movie.csv",index_col = 'movie_title') #創建第一個布爾條件 crit_a1 = movie.imdb_score > 8 crit_a2 = movie.content_rating == 'PG-13' crit_a3 = (movie.title_year < 2000) | (movie.title_year > 2009) final_crit_a = crit_a1 & crit_a2 & crit_a3 #創建第二個布爾條件 crit_b1 = movie.imdb_score < 5 crit_b2 = movie.content_rating == 'R' crit_b3 = (movie.title_year >= 2000) & (movie.title_year <= 2010) final_crit_b = crit_b1 & crit_b2 & crit_b3 #將兩個條件用或運算合并起來 final_crit_all = final_crit_a | final_crit_b #使用loc,對指定的列做過濾操作,可以清楚地看到過濾是否起作用 cols = ['imdb_score','content_rating','title_year'] movie_filtered = movie.loc[final_crit_all,cols] print(movie_filtered.head(10))
運行結果:
感謝各位的閱讀,以上就是“怎么使用Python的Pandas布爾索引”的內容了,經過本文的學習后,相信大家對怎么使用Python的Pandas布爾索引這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。