Reputation: 131
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
Reputation: 26676
Extract the first phrase in the string
df['Name'] =df['Name'].str.extract('(^\w+)')
Upvotes: 1
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