in Education by
I have the dataframe as below: df ID val 1 0.0 2 yes 3 1.0 4 0.0 5 yes How do I match my previous value with my current value if the column val equals "yes" I tried using the code below: df['val'] = df['val'].replace('yes', np.nan).bfill().astype(str) I am getting this output which is not what I wanted to. ID val 1 yes 2 yes 3 1.0 4 yes 5 yes can we use np.where along with the bfill? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You can use the below code: df.loc[df['val'].shift(-1).eq('yes'), 'val'] = 'yes' Output: ID val 0 1 yes 1 2 yes 2 3 1.0 3 4 yes 4 5 yes If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Related questions

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 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 am trying to combine 2 columns into my dataframe using a new column which is of type JSON. To do so i have used ... -----------+ Select the correct answer from above options...
asked Jan 11, 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
    I have the df df = pd.DataFrame({'col1': [1,2,3,4,5,6]}) and I would like to use df.isin() and ... help me find the solution to it? Select the correct answer from above options...
asked Jan 10, 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 the pandas dataframe that looks like this: df Date A B C 0 2020-08-17 1 1 0 1 2020-08-17 1 0 0 2 ... ... How do I achieve this? 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 have the function and want to create a new column df['growth_factor'] which will have a derived value in it. ... can I achieve this? 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
    I have the dataset that has the no_employees column that is the str object. whats is a best way to create the new ... 1-5 |Very Small Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a dataframe that looks like this: from to datetime other ---------------------------------- ... !! Thank you so much in advance! Select the correct answer from above options...
asked Feb 1, 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
...