Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to subtract string type columns values from another column in pandas

I have a data frame like this

df
col1         col2          col3
 A        black berry      black
 B        green apple      green
 C        red wine          red

I want to subtract col3 values from col2 values, the result will look like

df1
col1        col2        col3
  A         berry       black
  B         apple       green
  C          wine        red

How to do it in effective way using pandas

like image 610
Kallol Avatar asked Feb 22 '19 14:02

Kallol


People also ask

How do you subtract values from one DataFrame from another panda?

The sub() method of pandas DataFrame subtracts the elements of one DataFrame from the elements of another DataFrame. Invoking sub() method on a DataFrame object is equivalent to calling the binary subtraction operator(-). The sub() method supports passing a parameter for missing values(np. nan, None).

How do I subtract two columns in a pandas DataFrame?

We can create a function specifically for subtracting the columns, by taking column data as arguments and then using the apply method to apply it to all the data points throughout the column.

How do you subtract a string from a string in Python?

The Python string doesn't have a subtraction operator. If you want to get the difference of strings, here is a simple and understandable way. You can iterate all letters in a string using the for statement and check a string contains a letter by the if-in statement. The s is x - y or the difference of x and y .


1 Answers

Single line solution:

df["col2"] = df.apply(lambda x: x["col2"].replace(x["col3"], "").strip(), axis=1)
like image 77
Rocky Li Avatar answered Oct 19 '22 22:10

Rocky Li