Pandas dataframe apply function to column strings based on other column value

Testy8

I would like to remove all instance of the string in col 'B' from col 'A', like so:

col A                 col B    col C
1999 toyota camry     camry    1999 toyota 
2003 nissan pulsar    pulsar   20013 nissan

How would I do this using pandas? If it was a fixed value (non-dependent on another column), I would use:

df['col C'] = df['col A'].str.replace('value-to-replace','')
Jon Clements

Given a DataFrame of:

df = pd.DataFrame(
    {
        'A': ['1999 toyota camry', '2003 nissan pulsar'],
        'B': ['camry', 'pulsar']
    }
)

You can df.apply over the row axis and perform the replacement:

df['C'] = df.apply(lambda L: L.A.replace(L.B, ''), axis=1)

This'll give you:

                    A       B             C
0   1999 toyota camry   camry  1999 toyota 
1  2003 nissan pulsar  pulsar  2003 nissan 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Apply function to dataframe column element based on value in other column for same row?

From Dev

Apply function on dataframe Column to get several other columns Pandas Python

From Dev

How to apply a function to every value in a column in a pandas dataframe?

From Dev

how to select/add a column to pandas dataframe based on a function of other columns?

From Dev

Pandas: Create dataframe column based on other dataframe

From Dev

new python pandas dataframe column based on value of variable, using function

From Dev

Apply function on each column in a pandas dataframe

From Dev

pandas DataFrame, how to apply function to a specific column?

From Dev

Pandas dataframe apply function to entire column

From Dev

Pandas dataframe apply function to entire column

From Dev

Pandas: Create new column in DataFrame based on other column in DataFrame

From Dev

R: apply function to subsets based on column value

From Dev

Python pandas apply function if a column value is not NULL

From Dev

Create new column into dataframe based on values from other columns using apply function onto multiple columns

From Dev

Applying function to every other column in pandas dataframe

From Dev

Pandas DataFrame check colums value in other column

From Dev

pandas apply condition to column value based on another column

From Dev

Pandas DataFrame, replace value of a column by the value of an other column

From Java

pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

From Dev

Efficient way of scaling column based on value in other column in R dataframe

From Dev

Python: Pandas - Separate a Dataframe based on a column value

From Java

Deleting DataFrame row in Pandas based on column value

From Dev

Filter DataFrame based on Max value in Column - Pandas

From Dev

Pandas DataFrame manipulation based on Column value

From Dev

Repeat rows in a pandas DataFrame based on column value

From Dev

Pandas/Python: Set value of new column based on row value and other DataFrame

From Dev

DataFrame apply filtering by other column

From Dev

Pandas changing column value based on substring in other column

From Dev

DataFrame apply function based on multiple column and set value for multiple columns as well

Related Related

  1. 1

    Apply function to dataframe column element based on value in other column for same row?

  2. 2

    Apply function on dataframe Column to get several other columns Pandas Python

  3. 3

    How to apply a function to every value in a column in a pandas dataframe?

  4. 4

    how to select/add a column to pandas dataframe based on a function of other columns?

  5. 5

    Pandas: Create dataframe column based on other dataframe

  6. 6

    new python pandas dataframe column based on value of variable, using function

  7. 7

    Apply function on each column in a pandas dataframe

  8. 8

    pandas DataFrame, how to apply function to a specific column?

  9. 9

    Pandas dataframe apply function to entire column

  10. 10

    Pandas dataframe apply function to entire column

  11. 11

    Pandas: Create new column in DataFrame based on other column in DataFrame

  12. 12

    R: apply function to subsets based on column value

  13. 13

    Python pandas apply function if a column value is not NULL

  14. 14

    Create new column into dataframe based on values from other columns using apply function onto multiple columns

  15. 15

    Applying function to every other column in pandas dataframe

  16. 16

    Pandas DataFrame check colums value in other column

  17. 17

    pandas apply condition to column value based on another column

  18. 18

    Pandas DataFrame, replace value of a column by the value of an other column

  19. 19

    pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

  20. 20

    Efficient way of scaling column based on value in other column in R dataframe

  21. 21

    Python: Pandas - Separate a Dataframe based on a column value

  22. 22

    Deleting DataFrame row in Pandas based on column value

  23. 23

    Filter DataFrame based on Max value in Column - Pandas

  24. 24

    Pandas DataFrame manipulation based on Column value

  25. 25

    Repeat rows in a pandas DataFrame based on column value

  26. 26

    Pandas/Python: Set value of new column based on row value and other DataFrame

  27. 27

    DataFrame apply filtering by other column

  28. 28

    Pandas changing column value based on substring in other column

  29. 29

    DataFrame apply function based on multiple column and set value for multiple columns as well

HotTag

Archive