Replace data from one column by considering data from another in a Dataframe

Asked

Viewed 107 times

1

I have a Dataframe with two columns B and C where the data has no relation, would like the highlighted data of the column C received the highlighted column data B, considering the same index.

inserir a descrição da imagem aqui

After changing the Dataframe would look like this:

inserir a descrição da imagem aqui

  • 1

    Hello, what are you considering as a highlight? if it is a specific value you can use the apply method.

  • 2

    In your case I believe it would look like this;df['C'] = df.apply(lambda x: x['B'] if x['B'] == 'A' else x['C'] , axis=1)

1 answer

2


Straightforward.

Creating the Dataframe

>>> import pandas as pd

>>> df = pd.DataFrame({"A": [1, 2, 3, 4, 5, 6], "B": ["um", "dois", "a", "a", "cinco", "seis"], "C": ["a", "b", "outros", "outros", "c", "c"]})

>>> df
   A      B       C
0  1     um       a
1  2   dois       b
2  3      a  outros
3  4      a  outros
4  5  cinco       c
5  6   seis       c

Changing the value

>>> df.loc[df.B == "a", "C"] = "a"
>>> df
   A      B  C
0  1     um  a
1  2   dois  b
2  3      a  a
3  4      a  a
4  5  cinco  c
5  6   seis  c
>>>

With the help of Numpy

>>> import numpy as np

>>> df['C'] = np.where((df.B == 'a'), 'a', df.C)

>>> df
   A      B  C
0  1     um  a
1  2   dois  b
2  3      a  a
3  4      a  a
4  5  cinco  c
5  6   seis  c

I hope it helps

Browser other questions tagged

You are not signed in. Login or sign up in order to post.