Converting strings to a lower case in pandas [duplicate] Converting strings to a lower case in pandas [duplicate] python python

Converting strings to a lower case in pandas [duplicate]


df['url'] = df['url'].str.lower()

should operate on the series and replace it with the lower case version.


I think you need assign output back, better is omit apply if works only with column url:

df = pd.DataFrame({'url': ['www.CNN.com', 'www.Nbc.com', 'www.BBc.com', 'www.fOX.com'],                    'var1': ['XSD', 'wer', 'xyz', 'zyx']})print (df)           url var10  www.CNN.com  XSD1  www.Nbc.com  wer2  www.BBc.com  xyz3  www.fOX.com  zyx#if types of column is str, astype is not necessarydf.url = df.url.astype(str).str.lower()print (df)           url var10  www.cnn.com  XSD1  www.nbc.com  wer2  www.bbc.com  xyz3  www.fox.com  zyx

But if need convert all columns of df to lowercase strings:

df = df.astype(str).apply(lambda x: x.str.lower())print (df)           url var10  www.cnn.com  xsd1  www.nbc.com  wer2  www.bbc.com  xyz3  www.fox.com  zyx


To convert a single column we can use,

df.column_name.str.lower()

or

df['column_name'].str.lower()

Hope this helps !