in Education by
I am trying to create a new column where in, True if last n rows are True in other column. It works fine but the problem is it is taking lot of time. This is my code: dfx = pd.DataFrame({'A':[False,False,False,False,True,True,True,True,False,True]}) n=2 ## n to cover 10 min range samples cl_id = dfx.columns.tolist().index('A') ### cl_id for index number of the column for using in .iloc l1=[False]*n+[all(dfx.iloc[x+1-n:x+1,cl_id].tolist()) for x in np.arange(n,len(dfx))] dfx['B'] = l1 print(dfx) #old_col # New_col A B 0 False False 1 False False 2 False False 3 False False 4 True False 5 True True ## Here A col last two rows True, hence True 6 True True ## Here A col last two rows True, hence True 7 True True ## Here A col last two rows True, hence True 8 False False 9 True False Can anyone suggest me a better way to achieve it? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You need to use the pandas.Series.rolling: n = 2 dfx["A"].rolling(n).sum().eq(n) Output: 0 False 1 False 2 False 3 False 4 False 5 True 6 True 7 True 8 False 9 False Name: A, dtype: bool Use the benchmark against OP (about 1000x faster): dfx = pd.DataFrame({'A':[False,False,False,False,True,True,True,True,False,True]*1000}) %timeit -n10 l1 = dfx["A"].rolling(n).sum().eq(n) # 702 µs ± 88.6 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) %timeit -n10 l2 = [False]*n+[all(dfx.iloc[x+1-n:x+1,cl_id].tolist()) for x in np.arange(n,len(dfx))] # 908 ms ± 24 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) l1.tolist() == l2 # True If you are a beginner and want to know more about Python the do check out the python for data science course

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 have a big data frame. I want to replace float 1.0 and 0.0 with the true and false. My code: import pandas ... to do it in one line. Select the correct answer from above options...
asked Jan 19, 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 am trying to merge 2 data frames by using the id and then trying to save the result in the JSON file. | |id|a|b ... |3|15|3, 12|6, 15 Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    How do I use the for loop to execute the following operation? df_1=pd.concat([df_ab1,df_xy1], axis=1) df_2= ... Am I missing something? Select the correct answer from above options...
asked Jan 10, 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 have a set of oil wells compiled in the panda's data frame. It looks like this: wells = pd.DataFrame({'date ... -01-01 FIELDZ 10.8 Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    This is my function: def sub(number): tmp = [] tmp.append(number) while len(str(number)) > 1: tmp.append( ... do this in the python? Select the correct answer from above options...
asked Jan 9, 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 dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 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
    Is there any way to split the string if it contains an uppercase letter but not the first letter? For example, ... solution to it? Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I have the below data frame lst = [['A',1],['B',0],['C',1],['D',0],['E',1],['F',1],['G',1]] ... give all val with 1. df[df.val == 1] Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me what are the best Python IDEs for Data Science? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
...