1

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
....
Camilla
  • 111
  • 10
  • Does this answer your question? [How to split a dataframe string column into two columns?](https://stackoverflow.com/questions/14745022/how-to-split-a-dataframe-string-column-into-two-columns) – Nick ODell Dec 27 '21 at 21:03

3 Answers3

2

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

Afsana Khan
  • 122
  • 1
  • 12
1

Extract the first phrase in the string

 df['Name'] =df['Name'].str.extract('(^\w+)')
wwnde
  • 26,119
  • 6
  • 18
  • 32
0

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
Corralien
  • 109,409
  • 8
  • 28
  • 52