Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python's Pandas Boolean index

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use Python's Pandas Boolean index". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Python's Pandas Boolean index".

1. Calculate Boolean statistics

Import pandas as pd import numpy as np import matplotlib.pyplot as plt # reads movie and sets the row index as movie_title pd.options.display.max_columns = 50 movie = pd.read_csv (". / data/movie.csv" Index_col = 'movie_title') # determine whether the duration of a movie exceeds two hours # Figure1movie_2_hours = movie [' duration'] > 12 the total number of movies whose duration exceeds two hours print (movie_2_hours.sum ()) # result:1039# counts the proportion of movies with a duration of more than two hours print (movie_2_hours.mean ()) # Statistics the ratio of False to True (movie_2_hours.value_) Counts (normalize = True) # compare two columns of actors = movie [['actor_1_facebook_likes'] in the same DataFrame 'actor_2_facebook_likes']] .dropna () print ((actors [' actor_1_facebook_likes'] > actors ['actor_2_facebook_likes']) .mean ()) # Figure2

Running result:

Figure1

Figure2

two。 Build multiple Boolean conditions

Import pandas as pd import numpy as np import matplotlib.pyplot as plt # reads movie and sets the row index as movie_title pd.options.display.max_columns = 50 movie = pd.read_csv (". / data/movie.csv", index_col = 'movie_title') # create multiple Boolean conditions 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 ()) run result: Figure1 "# merge multiple Boolean conditions into one criteria_final = criteria1 & criteria2 & criteria3 print (criteria_final.head ()) # run result: Figure2

Running result:

Figure1

Figure2

3. Filter with Boolean index

Import pandas as pd import numpy as np import matplotlib.pyplot as plt # reads movie and sets the row index as movie_title pd.options.display.max_columns = 50 movie = pd.read_csv (". / data/movie.csv", index_col = 'movie_title') # create the first Boolean condition 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# creates the second Boolean condition crit_b1 = movie.imdb_score

< 5crit_b2 = movie.content_rating == 'R'crit_b3 = (movie.title_year >

= 2000) & (movie.title_year 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# creates the second Boolean condition crit_b1 = movie.imdb_score

< 5crit_b2 = movie.content_rating == 'R'crit_b3 = (movie.title_year >

= 2000) & (movie.title_year

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report