How to apply Lemmatization to a column in a pandas dataframe

daniel

If i had the following dataframe:

import pandas as pd

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

Output
          col1  col2
0  challenging     3
1     swimming     4

I am using the WordNetLemmatizer:

print(wordnet_lemmatizer.lemmatize('challenging',pos='v'))
print(wordnet_lemmatizer.lemmatize('swimming',pos='v'))

Output
challenge
swim

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

I have tried the following but no luck since it requires an input of pos so no change to dataframe

df['col1'] =df['col1'].apply(wordnet_lemmatizer.lemmatize)

If i try:

df['col1'] =df['col1'].apply(wordnet_lemmatizer.lemmatize(pos='v'))

I get

TypeError: lemmatize() missing 1 required positional argument: 'word'

The desired output is:

        col1  col2
0       challenge     3
1       swim     4
n1colas.m

Use the lambda function inside the apply to pass the word argument.

df['col1'] = df['col1'].apply(lambda word: wordnet_lemmatizer.lemmatize(word, pos='v'))
print(df)
        col1  col2
0  challenge     3
1       swim     4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to apply stemming to a column in a pandas dataframe

pandas DataFrame, how to apply function to a specific column?

How to apply LabelEncoder for a specific column in Pandas dataframe

How to apply an if between function on a column in pandas' DataFrame

Apply condition on dataframe column pandas

Pandas dataframe apply to multiple column

Apply a threshold on a Pandas DataFrame column

Apply condition for pandas dataframe column

Type Error during text lemmatization in Pandas Dataframe

How to apply a threshold to a pandas DataFrame column and output a row outside of the threshold?

How to apply a function to each row of one column in a pandas dataframe?

How to apply dictionary to a dataframe column that contains numpy array in pandas

How to split a pandas dataframe column into multiple using apply?

How to speed up Pandas apply function to create a new column in the dataframe?

How to apply pandas.qcut to each column in a dataframe of Python

How to apply an operationg to all column names of a Dataframe except one in Pandas?

How to apply a user-defined function to a column in pandas dataframe?

Pandas dataframe, how can I group by single column and apply sum to multiple column and add new sum column?

How to apply a function to a DataFrame column?

Pandas dataframe : how to apply slicing?

How to apply slicing to a pandas DataFrame?

how to apply a pandas series to a Dataframe?

How to apply a method to a Pandas Dataframe

How to apply formula to a dataframe in pandas

Pandas Dataframe Multiple column headers, apply function

Pandas DataFrame apply only returning first column

Apply a function to every column of a dataframe in pandas

Apply function on each column in a pandas dataframe

apply() to every column of every dataframe of an ExcelFile, Pandas