Camilla
Camilla

Reputation: 131

remove words from string in python

I have a column in pandas dataframe that looks like this:

Name
Apples 65xgb
Oranges 23hjkj
Bananas 76hhfk
....

Is it it anyway to get rid off of the ending of the string leaving only names of the product in the column?:

Name
Apples 
Oranges
Bananas
....

Upvotes: 1

Views: 326

Answers (3)

wwnde
wwnde

Reputation: 26676

Extract the first phrase in the string

 df['Name'] =df['Name'].str.extract('(^\w+)')

Upvotes: 1

Corralien
Corralien

Reputation: 120479

If you have a whitespace followed by a number, use:

# df = df.assign(Name=df['Name'].str.split('\s+\d+').str[0])
df['Name'] = df['Name'].str.split('\s+\d+').str[0]
print(df)

# Output
      Name
0   Apples
1  Oranges
2  Bananas

Upvotes: 0

Afsana Khan
Afsana Khan

Reputation: 122

df['Name'] = df['Name'].str.split().str[0]

Upvotes: 2

Related Questions