这篇文章主要讲解了“怎么使用Python的Pandas布尔索引”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么使用Python的Pandas布尔索引”吧!
计算布尔值统计信息
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')#判断电影时长是否超过两个小时 #Figure1movie_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
构建多个布尔条件
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 > 8criteria2 = 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
用布尔索引过滤
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 < 5crit_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_bprint(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 < 5crit_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布尔索引这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!