I have the following dataframe badges
. The Class
column has the level of the badge (1=Gold, 2=Silver, 3=Bronze)
. I want to get the time when the user with UserId = '699'
got his first silver medal. I tried the given code but I am getting a KeyError. Please help me fix this.
DataFrame
Id | UserId | Name | Date |Class | TagBased
2 | 23 | Autobiographer | 2016-01-12T18:44:49.267 | 3 | False
3 | 22 | Autobiographer | 2016-01-12T18:44:49.267 | 3 | False
4 | 21 | Autobiographer | 2016-01-12T18:44:49.267 | 3 | False
5 | 20 | Autobiographer | 2016-01-12T18:44:49.267 | 3 | False
6 | 19 | Autobiographer | 2016-01-12T18:44:49.267 | 3 | False
Code
test_silver = badges[badges.Date.loc[badges.UserId=='699']]
min(test_silver)
You can use boolean indexing (like you already did) and then head
to extract the first record:
badges.loc[badges.UserId.eq('699') & badges.Class.eq(2), 'Date'].head(1)
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.