Home > database >  If value in column of DataFrame exists, replace it. If not, append to DataFrame Python
If value in column of DataFrame exists, replace it. If not, append to DataFrame Python

Time:07-06

I have a DataFrame with 2 columns: ['a', 'b'] and values x and y. If x exists in 'a', I need to replace the value in column 'b' for that row with y. If not, append x, y to the end of the DataFrame.

I can use if x in df['a'].values() to return if it exists, but this does not help to replace that row's 'b' column with y. Is there a solution for this? Below is an example of the desired output:

x = 1
y = 5

df_before = 
| a | b |
| - | - |
| 0 | 6 |
| 3 | 2 |
| 2 | 8 |
| 1 | 2 |

df_after = 
| a | b |
| - | - |
| 0 | 6 |
| 3 | 2 |
| 2 | 8 |
| 1 | 5 |

df_before = 
| a | b |
| - | - |
| 0 | 6 |
| 3 | 2 |
| 2 | 8 |
| 5 | 3 |

df_after = 
| a | b |
| - | - |
| 0 | 6 |
| 3 | 2 |
| 2 | 8 |
| 5 | 3 |
| 1 | 5 |

CodePudding user response:

You can def your own function

def yourfunc(df,x,y):
    
    if df['a'].eq(x).any():
        df.loc[df['a'].eq(x),'b'] = y
    else : 
        df = pd.concat([df,pd.DataFrame([[x,y]],columns=df.columns)],ignore_index=True)
    return df

Or use combine_first after reset_index, which will update the value if the index match and append the new row when no match.

check = pd.DataFrame([[1,5]],columns=df.columns).set_index('a')
df = df.set_index('a').combine_first(check).reset_index()

CodePudding user response:

Not as elegant as BENY's solution, but should work.

x = 1
y = 5

df_length = len(df.axes[0])
i = 0

for j in range(df_length - 1):
    if(i > df_length):
        break
    elif(df.iloc[i]['a'] == x):
        df[i]['b'] = y
        i  = 1
    else:
        df = pd.concat([df,pd.DataFrame([[x,y]],columns=df.columns)],ignore_index=True)
        i  = 1
  • Related