Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas replace word begins with contains

df['sport'] = df.sport.str.replace(r'(^.*ball.*$)', 'ball sport')
df
Comment

pandas replace word begins with contains

In [71]:
df.loc[df['sport'].str.contains('ball'), 'sport'] = 'ball sport'
df

Out[71]:
    name       sport
0    Bob      tennis
1   Jane  ball sport
2  Alice  ball sport
Comment

pandas replace word begins with contains

df.sport = df.sport.apply(lambda x: 'ball sport' if 'ball' in x else x)
Comment

pandas replace word begins with contains

df.sport.str.replace(r'(^.*ball.*$)', 'ball sport')

0        tennis
1    ball sport
2    ball sport
Name: sport, dtype: object
Comment

PREVIOUS NEXT
Code Example
Python :: false in py 
Python :: repl.it secret 
Python :: repl.it install packages python 
Python :: python remove first item in list 
Python :: cv2 videowriter python not working 
Python :: drf model methods serializer 
Python :: how to get csv file first row first column value in python 
Python :: find and replace subword in word python regex 
Python :: how to devided array into parts python 
Python :: functools.cached_property objects in python 
Python :: torch.stack 
Python :: numpy cumsum 
Python :: django MESSAGE_TAGS 
Python :: index of and last index of in python 
Python :: create and activate virtual environment with python 3 
Python :: pygame rect 
Python :: python xmlrpc 
Python :: how to do input python 
Python :: create tuples in pandas 
Python :: add title to tkinter window python 
Python :: python initialize multidimensional array 
Python :: python codes for counting the occurrence of a letters in dictionary excluding digits 
Python :: convert 2 lists into dictionary 
Python :: a function to create a null matrix in python 
Python :: qr detector 
Python :: Accessing Elements from Dictionary 
Python :: print string in reverse order uing for loop python 
Python :: if statement python explained 
Python :: num2words python 
Python :: How to use Counter() Function 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =