2

I have a dataframe like this

df:
col1                      col2
blue water bottle        blue
red wine glass           red
green cup                green 

I want make another column which will ignore the value of col2 from col1 for example the new column col3 will be:

water bottle
wine glass
green cup

I have tried this code:

df.apply(lambda x: x['col1'].replace(x['col2'], ''), axis=1)

But I am getting following error:

AttributeError: ("'NoneType' object has no attribute 'replace'", 'occurred at index 0')

How to do it ?

1
  • df["col3"] = df.apply(lambda x: x["col1"].replace(x["col2"], ""), axis=1) should work. My guess is you have None value somewhere. You could check with df[df.col1.isnull()] Commented Dec 29, 2018 at 8:20

4 Answers 4

2

The reason is that the "col1" for some rows in your dataframe are None. You will need to handle those cases for example by assigning an empty string to col3

df["col3"] = df.apply(
    lambda x: "" if pd.isnull(x["col1"]) else x["col1"].replace(x["col2"], ""),
    axis=1
)
Sign up to request clarification or add additional context in comments.

Comments

1

Use -

df[['col','col2']].apply(lambda x: x[0].replace(x[1],''), axis=1)

Output

0     water bottle
1       wine glass
2              cup
dtype: object

Comments

0

Drop rows with NaN entries before apply your lambda: df[['col1', 'col2']].dropna().apply(lambda x: x['col1'].replace(x['col2'], ''), axis=1)

Comments

-1

This is one way (vectorizing would give a better answer of course)

import pandas as pd

df = pd.DataFrame()
df['col'] = ['blue water bottle', 'red wine glass', 'green cup']
df['col2'] = ['blue', 'red', 'green']
df['col3'] = ['', '', '']
for idx, row in df.iterrows():
    row['col3'] = row['col'].replace(row['col2'], '').strip()

pandas string replace

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.