简体   繁体   中英

Boolean indexing in Pandas

I have the following DataFrame

books = pd.Series(data = ['Great Expectations', 'Of Mice and Men', 'Romeo and Juliet', 'The Time Machine', 'Alice in Wonderland' ])
authors = pd.Series(data = ['Charles Dickens', 'John Steinbeck', 'William Shakespeare', ' H. G. Wells', 'Lewis Carroll' ])

user_1 = pd.Series(data = [3.2, np.nan ,2.5])
user_2 = pd.Series(data = [5., 1.3, 4.0, 3.8])
user_3 = pd.Series(data = [2.0, 2.3, np.nan, 4])
user_4 = pd.Series(data = [4, 3.5, 4, 5, 4.2])
dict_temp = {'Book Title':books, 'Author': authors, 'User 1': user_1, 'User 2':user_2, 'User 3': user_3, 'User 4': user_4}
pd.set_option('precision', 1)
temp_df = pd.DataFrame(dict_temp)

My aim is to select all the columns which have user rating = 5.0. When i do the following it works fine.

temp_df[temp_df == 5.0] 

However, if i want to select columns which have user rating > 4.0, the result is different. Why is this?

temp_df[temp_df > 4.0]

Here is the screenshot of what happens when I run temp_df == 5.0 v/s temp_df > 4.0. My question is why do i see Book Title and Author columns在此处输入图片说明 ?

PS i am able to acheive my desired results by this line

temp_df[temp_df[['User 1','User 2','User 3','User 4']] > 4.0]

I have no problem running the following with your code.

I explicitly set the 4.0 to a float which may help you, though for me it was not an issue.

temp_df = pd.DataFrame(dict_temp)
print(temp_df)
temp_df = temp_df[temp_df > float(4.0)]
print(temp_df)

Output

[5 rows x 6 columns]
            Book Title               Author   ...    User 3  User 4
0   Great Expectations      Charles Dickens   ...       NaN     NaN
1      Of Mice and Men       John Steinbeck   ...       NaN     NaN
2     Romeo and Juliet  William Shakespeare   ...       NaN     NaN
3     The Time Machine          H. G. Wells   ...       NaN     5.0
4  Alice in Wonderland        Lewis Carroll   ...       NaN     4.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM