I have a data frame like this:
ID | Date | Rank | Info |
---|---|---|---|
12 | 12/5 | 1 | Anne |
12 | 12/5 | 2 | Louis |
12 | 12/5 | 3 | Nico |
12 | 12/5 | 4 | Vic |
12 | 12/5 | 5 | Vinci |
12 | 15/5 | 1 | Nico |
12 | 15/5 | 2 | Stephanie |
12 | 15/5 | 3 | Athene |
12 | 15/5 | 4 | Louis |
ID test12: I want to select all persons below second place and above fifth place on 12/5; and 15/5, all persons below first rank and above fouth place.
I declared a corresponding array [[2,5],[1,4]]
output: [Nico; Vic] and [Stephanie; Athene]
How can i do?
Thank you so much!!!!!
CodePudding user response:
You can use between()
and then groupby
:
# between(inclusive,exclusive) as default
df.loc[df.Rank.between(3,4)].groupby('Date').agg({'Info':list})
prints:
Info
Date
12-May [Nico, Vic]
15-May [Athene, Louis]
CodePudding user response:
Asuming you are using a pandas dataframe the following should work. I am also asuming by "below second place and above fifth place" you mean 3,4. If not adjust where necesary. This is what should work to get Nico and Vic as output.
dataframe[(dataframe['Rank'] > 2) & (dataframe['Rank'] < 5) & (dataframe['Date'] == '12/5')]
In essence what you are doing is filtering out all the dates and rank you dont want and returning the dataframe based on the values you filtered out. You can use the same process to get the output on the second use case.