39

I have a data that contains domain names:

 url          var1
www.CNN.com   xsd
www.Nbc.com   wer
www.BBc.com   xyz
www.fOX.com   zyx
....

The data is of the Series type. I am using the following to convert url variable to lower case:

df.apply(lambda x: x.astype(str).str.lower())

However, they remain the same.

What am I doing wrong?

cs95
  • 379,657
  • 97
  • 704
  • 746
Feyzi Bagirov
  • 1,292
  • 4
  • 28
  • 46

3 Answers3

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

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

David
  • 1,224
  • 10
  • 20
  • 2
    got an error `//anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:3: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy app.launch_new_instance()` – Feyzi Bagirov Mar 12 '17 at 17:25
  • @FeyziBagirov I didn't get that in my test data set that I tried to mirror yours (also python 3.5). Can you post code in your question for generating the data set just so we have the same input? – David Mar 12 '17 at 17:31
  • @FeyziBagirov you may try `df['url'] = df.loc[:,'url'].str.lower()` but I concur with David it's unnecessary here – Sergey Bushmanov Mar 12 '17 at 17:36
  • 2
    @FeyziBagirov: for me syntax like: `df.loc[:, 'url'] = df.loc[:, 'url'].str.lower()` finally worked. – izkeros Dec 10 '19 at 07:02
10

To convert a single column we can use,

df.column_name.str.lower()

or

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

Hope this helps !

Wickkiey
  • 4,446
  • 2
  • 39
  • 46
9

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 var1
0  www.CNN.com  XSD
1  www.Nbc.com  wer
2  www.BBc.com  xyz
3  www.fOX.com  zyx

#if types of column is str, astype is not necessary
df.url = df.url.astype(str).str.lower()
print (df)
           url var1
0  www.cnn.com  XSD
1  www.nbc.com  wer
2  www.bbc.com  xyz
3  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 var1
0  www.cnn.com  xsd
1  www.nbc.com  wer
2  www.bbc.com  xyz
3  www.fox.com  zyx
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • The first solution generated an error `//anaconda/lib/python3.5/site-packages/pandas/core/generic.py:2701: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self[name] = value` The second solution works though – Feyzi Bagirov Mar 12 '17 at 17:24
  • I think problem is in code above this row, can you share 2,3 rows above? – jezrael Mar 12 '17 at 17:26
  • Or maybe check [docs](http://pandas.pydata.org/pandas-docs/stable/indexing.html#returning-a-view-versus-a-copy) – jezrael Mar 12 '17 at 17:36
  • I add test sample, do you have error with it? – jezrael Mar 12 '17 at 17:48