0

I have A dataframe like below,

df = pd.Dataframe({'Col1' : pd.Series(['Abc','Cde','Efg','Abc'], index=['a', 'b', 'c','d']),
 'Col2' : pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd']),
 'Col3' : pd.Series([1, 2., 3., 4.], index=['a', 'b', 'c', 'd'])})

By Depending on the value of the column value in Col1, I want to replace Col3 Value by Col2,

In this case where Col1 value is "Abc" I want to update Col3 value with Col2 value , expecting output like

pd.Dataframe({'Col1' : pd.Series(['Abc','Cde','Efg','Abc'], index=['a', 'b', 'c','d']),
 'Col2' : pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd']),
 'Col3' : pd.Series([10, 2., 3., 40], index=['a', 'b', 'c', 'd'])})

Tried by filter , that aint generic, so any proper method to do same !!

0

1 Answer 1

5

This should work:

import pandas as pd

df = pd.DataFrame({'Col1' : pd.Series(['Abc','Cde','Efg','Abc'], index=['a', 'b', 'c','d']),
                   'Col2' : pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd']),
                   'Col3' : pd.Series([1, 2., 3., 4.], index=['a', 'b', 'c', 'd'])})

df.loc[df['Col1'] == 'Abc', 'Col3'] = df['Col2']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.