how to set value in 2 cell if other cell contains 'something'

j.stalin

I have some pandas dataframe:

a,b,c
AAA,,
DDD,,
KKK,,
AAA,,  

I want to search in column "A" and if string in column "A" contains word "AAA" I need set value "BBB" in column "B" and "CCC" in column "C".
So, I want to get result as following:

a,b,c
AAA,BBB,CCC
DDD,,
KKK,,
AAA,BBB,CCC

I wrote the code with numpy:

df['b'] = pd.np.where(df.a.str.contains("AAA"), "BBB", '')

How to extend it to work with 'b' and 'c' columns?

jezrael

You can use double np.where:

mask = df.a.str.contains("AAA")
df['b'] = pd.np.where(mask, "BBB", '')
df['c'] = pd.np.where(mask, "CCC", '')

Or assign:

mask = df.a.str.contains("AAA")
df = df.assign(b=pd.np.where(mask, "BBB", ''), c=pd.np.where(mask, "CCC", ''))

If need create multiple columns with one np.where is necessary create Nx1 mask:

mask = df.a.str.contains("AAA")[:, None]
df[['b','c']] = np.where(mask, ['BBB','CCC'], ['',''])
print (df)
     a    b    c
0  AAA  BBB  CCC
1  DDD          
2  KKK          
3  AAA  BBB  CCC

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jquery change table cell value if other cell contains word

From Dev

How to set some text to datagridview cell when its cell contains 0 value in c#?

From Dev

How to set some text to datagridview cell when its cell contains 0 value in c#?

From Dev

Excel cell contains reference to other cell. Set a range to point at the other cell

From Dev

Set cell value based on other cell's information

From Dev

How to change value of one cell onchange of other cell value in DataGridview

From Dev

How to change cell value repeatedly by entering the value in other cell or sheet?

From Dev

How to SUM values in cells if other cell in each row contains a particular value?

From Dev

How to change background color of cell based on other cell value by VBA

From Dev

how to make cell editable based on other cell value

From Dev

If cell contains partial string, then change cell value

From Dev

insert cell next to a cell that contains a certain value

From Dev

How to set the value of a cell to Sum of the cell and e.parameter

From Dev

Excel returns TRUE on comparison of two columns. However one cell contains value 0 and other cell is empty

From Dev

How to set Cell value of DataGridViewRow by column name?

From Dev

How to set the value of specific cell in JTable?

From Dev

How to set Cell value of DataGridViewRow by column name?

From Dev

How to set value to non-empty cell

From Dev

How to return a value from a range if cell contains that value

From Dev

How to return a value from a range if cell contains that value

From Java

Check if list cell contains value

From Dev

If cell contains value then 'Column header'

From Dev

Set cell value with VBA

From Dev

Set a string to a value in a cell?

From Dev

Locking cell based on value on other cell

From Dev

Use value from cell to determine other cell

From Dev

Locking cell based on value on other cell

From Dev

Clear contents of cell based on value in other cell

From Dev

Change color for specific number of cells by value set in other cell

Related Related

  1. 1

    Jquery change table cell value if other cell contains word

  2. 2

    How to set some text to datagridview cell when its cell contains 0 value in c#?

  3. 3

    How to set some text to datagridview cell when its cell contains 0 value in c#?

  4. 4

    Excel cell contains reference to other cell. Set a range to point at the other cell

  5. 5

    Set cell value based on other cell's information

  6. 6

    How to change value of one cell onchange of other cell value in DataGridview

  7. 7

    How to change cell value repeatedly by entering the value in other cell or sheet?

  8. 8

    How to SUM values in cells if other cell in each row contains a particular value?

  9. 9

    How to change background color of cell based on other cell value by VBA

  10. 10

    how to make cell editable based on other cell value

  11. 11

    If cell contains partial string, then change cell value

  12. 12

    insert cell next to a cell that contains a certain value

  13. 13

    How to set the value of a cell to Sum of the cell and e.parameter

  14. 14

    Excel returns TRUE on comparison of two columns. However one cell contains value 0 and other cell is empty

  15. 15

    How to set Cell value of DataGridViewRow by column name?

  16. 16

    How to set the value of specific cell in JTable?

  17. 17

    How to set Cell value of DataGridViewRow by column name?

  18. 18

    How to set value to non-empty cell

  19. 19

    How to return a value from a range if cell contains that value

  20. 20

    How to return a value from a range if cell contains that value

  21. 21

    Check if list cell contains value

  22. 22

    If cell contains value then 'Column header'

  23. 23

    Set cell value with VBA

  24. 24

    Set a string to a value in a cell?

  25. 25

    Locking cell based on value on other cell

  26. 26

    Use value from cell to determine other cell

  27. 27

    Locking cell based on value on other cell

  28. 28

    Clear contents of cell based on value in other cell

  29. 29

    Change color for specific number of cells by value set in other cell

HotTag

Archive