Ahamed Moosa
Ahamed Moosa

Reputation: 1445

Python singularize words from pandas dataframe

I want to convert the plural words from my column "Phrase" into singular word. How do I iterate over each row and each item?

my_data = [('Audi Cars', 'Vehicles'),
           ('Two Parrots', 'animals'),
           ('Tall Buildings', 'Landmark')]
test = pd.DataFrame(my_data)
test.columns = ["Phrase","Connection"]
test

I tried

test["Phrase"] = test["Phrase"].str.lower().str.split()
import inflection as inf
test["Phrase"].apply(lambda x:inf.singularize([item for item in x]))

My desired output is

Phrase:         Connection:
Audi Car        Vehicles
Two Parrot      animals
Tall Building   Landmark

Kindly note, I want to singularize only one column Phase

Upvotes: 1

Views: 2971

Answers (1)

Vivek Kalyanarangan
Vivek Kalyanarangan

Reputation: 9081

Slight change -

test['clean'] = test['Phrase'].apply(lambda x: ' '.join([inf.singularize(item) for item in x.split()]))

Output

           Phrase Connection          clean
0       Audi Cars   Vehicles       Audi Car
1     Two Parrots    animals     Two Parrot
2  Tall Buildings   Landmark  Tall Building

Explanation

In your existing code, you are doing this -

test["Phrase"].apply(lambda x:inf.singularize([item for item in x]))

Let us take the first example and see what happens. x in this case will be Audi Cars -

[item for item in x] returns a list of characters - ['A', 'u', 'd', 'i', ' ', 'C', 'a', 'r', 's'] so singularize doesn't work as it is only on characters.

Trick was to do x.split() which splits words and then put the singularize inside the list comprehension.

Finally do a ' '.join() to get back the string.

Upvotes: 2

Related Questions