Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to apply stemming to a column in a pandas dataframe

If i had the following dataframe:

import pandas as pd

d = {'col1': ['goodness', 'beautiful'], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

Output
        col1  col2
0   goodness     3
1  beautiful     4

I am using the porter stemmer:

print(porter.stem('goodness'))
print(porter.stem('beautiful'))

Output
good
beauti

How can I apply this stem function to all elements of col1 from the original dataframe?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have tried the following but no luck since it requires an input of word

df['col1'].apply(porter.stem(word), arg= word for word in df['col1'])

The desired output is:

        col1  col2
0       good     3
1       beauti     4

>Solution :

df['col1'] = df['col1'].apply(porter.stem)

should do the job.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading