I'm trying to take the value in a column in pandas and remove that value from another column. However - the replace behavior is not working the way I would have expected.
In this example, I am trying to make the value in col2 equal to 'something'
import pandas as pd
#Build the dataframe
col1 = ['ABC', 'DEF']
col2 = ['something - ABC', 'something - DEF']
df1 = pd.DataFrame(['ABC', 'DEF'], columns = ['col1'])
df2 = pd.DataFrame(['something - ABC', 'something - DEF'], columns = ['col2'])
df = df1.join(df2, on=None, how='left')
#Replace ' - ABC' so column is just 'something'
df['newcolumn'] = df.col2.replace(' - '+df.col1, '')
This is returning the value that's already in col2. What am I missing?
-followed by something you want to replace? Furthermore, will this pattern always remain at the end?str.replace. Which I guarantee will be slow.