Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a column by groupby Pandas DataFrame based on tail(1).index

I want create a boolean column that said if a match on first or second half for each match in the dataframe.
Code

#First Half
firsthalf_index = df.groupby(['Date','Match']).apply(lambda x: x[(x.M >= 1) & (x.M <= 45)].tail(1).index)

#Second Half
secondhalf_index = df.groupby(['Date','Match']).apply(lambda x: x[(x.M >= 46) & (x.M <= 90)].tail(1).index)

This code return only the referred.index for each game

Output
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What I want to add in this code is df[df.index < firsthalfindex_index] and df[(df.index > firsthalfindex.index) & (df.index < secondhalf_index)]

>Solution :

You could do

firsthalf_index = ((df.M >= 1) & (df.M <= 45)).iloc[::-1].groupby([df['Date'],df['Match']]).transform('idxmax')
secondhalf_index =((df.M >= 46) & (df.M <= 90)).iloc[::-1].groupby([df['Date'],df['Match']]).transform('idxmax')

Then

s = df.index.to_series()
df[(s > firsthalf_index) & (s < secondhalf_index)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading