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
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).
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.
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 .
Single line solution:
df["col2"] = df.apply(lambda x: x["col2"].replace(x["col3"], "").strip(), axis=1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With