Pandas duplicating row if condition met, and assigning value

Hassan A

I have a Pandas dataframe like the one below, where column A is a series of string values, and column B maintains a running total of the number of times the value in column A differs from the value of column A in the previous row.

A    B       
1    1          
1    1             
1b   2          
1b   2                
1b   2    
1    3   

Every time there is a change in the value of column A, I would like to duplicate the preceding row and assign it an incremented value of column B. For example, with the input dataframe as above, the output would look like:

A    B       
1    1          
1    1   
1    2            
1b   2          
1b   2                
1b   2 
1b   3    
1    3   

Any thoughts about how to go about this in an efficient way?

jezrael

Filter last duplicated values by B, then shifting only B and assign back, remove last row and last join togehter by concat with sorting by index:

df1 = (df[df['B'].ne(df['B'].shift(-1))]
         .assign(B = lambda x: x.B.shift(-1)).iloc[:-1].astype({'B':int}))

df = pd.concat([df, df1]).sort_index(ignore_index=True)
print (df)
    A  B
0   1  1
1   1  1
2   1  2
3  1b  2
4  1b  2
5  1b  2
6  1b  3
7   1  3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas: Replace value of column with previous row value if condition is met

Getting Value Error when assigning row based on multiple condition in Pandas

PANDAS : Assigning a value for each row in certain column based on multiple if condition

Return value from the row below when condition is met in pandas

Add quantity to pandas column if row condition is met

How to add value to previous row if condition is met

Update row value with column name if condition is met

Assigning a value to a new Pandas Column on given condition

pandas : update value if condition met in loop

Evaluate a condition in a row and return a certain value if the condition is met

How to replace a pandas column row with the previous row if a condition is met

How to change the value in a row if condition met in the previous row under dplyr

Repeat row of dataframe if condition met, and change value of one value

Count row if condition is met

Skip a row when condition is not met using pandas apply

Update row values where certain condition is met in pandas

Pandas - if condition is met in a row, add values to preceding rows without iteration

Copy and split row by if cell condition it met - Pandas Python

When a condition is met, show current row value on each line below it, where the condition is not met

Is there a way to store previous value in a row and change when a new condition is met?

PostgreSQL delete row if condition met, else update some value

How to get value from upcomming row if condition is met?

Get value in other cell in same row when if condition in for loop is met

Pandas: Add new column and assigning value from another dataframe by condition

Add value or text to column a if condition in column b is met pandas

pandas copy value from one column to another if condition is met

When a condition is met in a pandas column return the value of another column

Trying to return value from column in pandas if condition in another column is not met

Insert value 1 in pandas Dataframe till one condition is met [Python]