in Education by
I have the below data frame lst = [['A',1],['B',0],['C',1],['D',0],['E',1],['F',1],['G',1]] df = pd.DataFrame(lst,columns = ['name','val']) Looks like this name val 0 A 1 1 B 0 2 C 1 3 D 0 4 E 1 5 F 1 6 G 1 I am trying to get rows where val is 1 but they should be bottom continuous rows. Desired output name val 4 E 1 5 F 1 6 G 1 I am doing, which will give all val with 1. df[df.val == 1] Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You can use cumsum to get the last block: # blocks separated by `0` s = df.val.ne(1).cumsum() # last blocks and only value 1 df[s.eq(s.max()) & df['val'].eq(1)] Output: name val 4 E 1 5 F 1 6 G 1 Learn Python for Data Science Course to improve your technical knowledge.

Related questions

0 votes
    I have this dataframe and trying to select the last n (=2) rows if the present value is True so I code as ... , I should select 50,40 Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I am from an SQL background was looking for the best way of doing this. c = a.account_id.isin(b. ... above method efficient way? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    I want to select rows from a DataFrame based on values in some column in pandas, How can I do it? I ... WHERE column_name = some_value Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I am trying to groupby a column and compute value counts on another column. import pandas as pd dftest = pd. ... Amt, already exists Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", ... Any hints would be welcome. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with a column designated to town names. After each town name, I am adding a word " ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with the column designated to town names. After each town name, I am adding a word ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I am trying to alter the data in the panda's df. Using below, where X >=5, I want to change the corresponding Y row to 1. Where X...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have 2 data frames df1 Name 2010 2011 0 Jack 25 35 1 Jill 15 20 df2 Name 2010 2011 0 Berry 45 25 1 ... used the code df1.add(df2) Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    To the data frame df: Player Team Points Mean Price Value Gameweek 1 Jim Leeds 4.4 4.40 10.44 0.44 2 Jim ... scalar What am I missing? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    I have the list like this where items are separated by ":". x=['john:42:engineer', 'michael:29:doctor' ... engineer 1 michael 29 doctor Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I have 2 columns in the python dataframe. I want to check each row in my Column A for any value that ... for this particular purpose. Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have this following datetime64[ns] type timestamps. x1=pd.Timestamp('2018-04-25 00:00:00') x2=pd.Timestamp('2020- ... -04-25 00:53:00 Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I am trying to transpose from df1 to df2 in pandas. Can anyone help me with the codes that transpose data from wide to ... /25/20 4 6 8 Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
...