Home > OS >  remove words from string in python
remove words from string in python

Time:12-28

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
....

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

Extract the first phrase in the string

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