Copy value from one column based on the value of another column

Nicholas Tulach

I'm trying to fill values in one column from two other columns based on the values in a fourth column.

I have a pandas dataframe with four columns: A, B, C, D

df_copy = df.copy()
for i, row in df.iterrows():
    if 'Test' in row.D:
        df_copy.loc[i, 'A'] = row.B
    elif 'Other' in row.D:
        df_copy.loc[i, 'A'] = row.C

This works, but is very slow. Is there a more efficient way?

joris

You can use 'boolean indexing' for this instead of iterating over all rows:

df_copy.loc[df['D']=='Test', 'A'] = df['B']
df_copy.loc[df['D']=='Other', 'A'] = df['C']

If you know that column D only consists of these two values, it can even shorter:

df_copy['A'] = df['B']
df_copy.loc[df['D']=='Other', 'A'] = df['C']

If you want to have the same as the in operator to test if that substring is in the column, you can do:

df['D'].str.contains('Other')

to become the boolean values instead of the df['D']=='Other'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy value from one column based on the value of another column

From Dev

Updating one column based on the value of another column

From Dev

Copy Column Value from One table into Another Matching IDs

From Dev

Copy timestamp value fails from one column to another

From Dev

r - copy value based on match in another column

From Dev

Update column value from one position to another position based on indexed

From Dev

move value from one column to another based on condition

From Java

Pandas/Python: Set value of one column based on value in another column

From Dev

Pandas/Python: Set value of one column based on value in another column

From Dev

Pandas assign value of one column based on another

From Dev

How to get every value from one column to another column based on its corresponding value

From Dev

Power Shell copy value of one column into a column in another list

From Dev

Excel- How to only copy and paste a "specific" string value from one column to another column?

From Dev

UPDATE sql column with value from another column based on a date column

From Dev

Need to find value in one column and then return value from another column

From Dev

Copy a value from one sheet to another and then copy result back to same sheet in different column

From Dev

SELECT Mysql - Replacing value in one column based on another column

From Dev

Flag a row in one column based on past value in another column

From Dev

Replace NaN's in one column with string, based on value in another column

From Dev

groupby and withhold information of one column based on value of another column

From Dev

Replace NaN's in one column with string, based on value in another column

From Dev

Setting the value of one column based on three conditions of another column

From Dev

Change value from one column into another

From Dev

updating column value based on another column value

From Dev

MySQL query to sum one column and count another column, from two tables, based on a common value?

From Dev

Replacing an empty cell in one column based on corresponding value from another column?

From Dev

R dplyr - select values from one column based on position of a specific value in another column

From Dev

Joining one column from a file to another file based on matched value of first column in both

From Dev

copy one row from a table to another and insert value of one column in same query

Related Related

  1. 1

    Copy value from one column based on the value of another column

  2. 2

    Updating one column based on the value of another column

  3. 3

    Copy Column Value from One table into Another Matching IDs

  4. 4

    Copy timestamp value fails from one column to another

  5. 5

    r - copy value based on match in another column

  6. 6

    Update column value from one position to another position based on indexed

  7. 7

    move value from one column to another based on condition

  8. 8

    Pandas/Python: Set value of one column based on value in another column

  9. 9

    Pandas/Python: Set value of one column based on value in another column

  10. 10

    Pandas assign value of one column based on another

  11. 11

    How to get every value from one column to another column based on its corresponding value

  12. 12

    Power Shell copy value of one column into a column in another list

  13. 13

    Excel- How to only copy and paste a "specific" string value from one column to another column?

  14. 14

    UPDATE sql column with value from another column based on a date column

  15. 15

    Need to find value in one column and then return value from another column

  16. 16

    Copy a value from one sheet to another and then copy result back to same sheet in different column

  17. 17

    SELECT Mysql - Replacing value in one column based on another column

  18. 18

    Flag a row in one column based on past value in another column

  19. 19

    Replace NaN's in one column with string, based on value in another column

  20. 20

    groupby and withhold information of one column based on value of another column

  21. 21

    Replace NaN's in one column with string, based on value in another column

  22. 22

    Setting the value of one column based on three conditions of another column

  23. 23

    Change value from one column into another

  24. 24

    updating column value based on another column value

  25. 25

    MySQL query to sum one column and count another column, from two tables, based on a common value?

  26. 26

    Replacing an empty cell in one column based on corresponding value from another column?

  27. 27

    R dplyr - select values from one column based on position of a specific value in another column

  28. 28

    Joining one column from a file to another file based on matched value of first column in both

  29. 29

    copy one row from a table to another and insert value of one column in same query

HotTag

Archive