remove words = ['apple','banana']
col A
apple is good
after removal
col A
is good
col removed word
apple
CodePudding user response:
import pandas as pd
remove_words = ['apple','banana']
df = pd.DataFrame([
['apple is good'],
['banana is bad'],
['banana is good and apple is bad']
], columns=['col A'])
print(df)
df['col removed word'] = df['col A'].apply(
lambda t: [w for w in remove_words if w in t.split()]
)
df['col A'] = df['col A'].apply(
lambda t: ' '.join([w for w in t.split() if w not in remove_words])
)
print(df)
prints
index | col A |
---|---|
0 | apple is good |
1 | banana is bad |
2 | banana is good and apple is bad |
index | col A | col removed word |
---|---|---|
0 | is good | apple |
1 | is bad | banana |
2 | is good and is bad | apple,banana |