Pandas: Count Higher Ranks For Current Experiment Participants In Later Experiments (Part 1)

2024/10/5 19:17:19

Learning Experiments

In a series of learning experiments, I would like to count the number of participants in each experiment that improved their performance in subsequent experiments (Rank 1 is highest). In addition, I would also like to count the number of participants in each experiment that subsequently reached the top rank.

Here is a short, sanitized version of the learning experiment csv file that I have loaded into a pandas dataframe (df_learning).

Experiment Subject Rank
A Alpha 1
A Bravo 2
A Charlie 3
A Delta 4
A Echo 5
B Alpha 1
B Charlie 2
B Echo 3
B Foxtrot 4
B Golf 5
B India 6
B Juliet 7
C Juliet 1
C Bravo 2
C Charlie 3

Please advise?

Answer

You can use a groupby.cummax, then boolean indexing:

m = df['Rank'].sub(df.groupby('Subject')['Rank'].cummax()).lt(0)improved_rank = df.loc[m, 'Subject'].unique()

output: ['Charlie', 'Echo', 'Juliet']

reached_top_rank = df.loc[m&df['Rank'].eq(1), 'Subject'].unique()

output: ['Juliet']

https://en.xdnf.cn/q/119184.html

Related Q&A

Pass variables into and out of exec [closed for not being clear. Modified and reuploaded] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

List Object Not Callable, Syntax Error for Text-Based RPG

Im having issues (still) with generating a random object, in this case a random herb found by foraging. Heres the code for the function:def collectPlants(self):if self.state == normal:print"%s spe…

Why does my Tkinter window background not change?

Ive a small program with a feature to change the background color of a different window than the frame I use to ask for the background color. (I hope you understand this.) The program is written in Pyt…

Combining a nested list without affecting the key and value direction in python

I have a program that stores data in a list. The current and desired output is in the format:# Current Input [{Devices: [laptops, tablets],ParentCategory: [computers, computers]},{Devices: [touch], Par…

Splitting very large csv files into smaller files

Is Dask proper to read large csv files in parallel and split them into multiple smaller files?

How to make this Python script to run subfolders too?

Which part of the codes do I need to change in order to include subfolders? File handle.py import glob import os import sys from typing import Listdef get_filenames(filepath: str, pattern: str) -> …

Why doesnt this recursive GCD function work? [duplicate]

This question already has answers here:Why does my recursive function return None?(4 answers)What is the purpose of the return statement? How is it different from printing?(15 answers)Closed 1 year …

How can I append \n at the end of the list in list comperhansion

I have this code:def table(h, w):table = [[. for i in range(w)] for j in range(h)]return tablewhich returns this[ [., ., .], [., ., .], [., ., .], [., ., .] ]How to make it return this?[[., ., .],[., …

Seaborn bar plot y axis has different values than expected

The y values in the Seaborn barplot are different than what my data shows.My data shows:yearmonth 2018-10 763308.0 2018-11 708366.0 2018-12 703952.0 2019-01 844039.0 2019-02 749583.…

Merge multiple JSON into single one (Python)

I am searching for a way to merge multiple JSONs into a single one. My output is in this format:[{"Nome bollettino": "Bollettino 1"}, {"Causale": "1"}, {"Nu…