tonyibm
tonyibm

Reputation: 641

how to change upper cases for all values in dataframe

i want to change upper cases for all values in dataframe, and use the following codes,

import pandas as pd
import numpy as np

path1= "C:\\Users\\IBM_ADMIN\\Desktop\\ml-1m\\SELECT_FROM_HRAP2P3_SAAS_ZTXDMPARAM_201611291745.csv"
frame1 = pd.read_csv(path1,encoding='utf8',dtype = {'COUNTRY_CODE': str})
for x in frame1:
    frame1[x] = frame1[x].str.lower()
frame1

but i get the following error for this row:

 frame1[x] = frame1[x].str.lower()

error:

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

don't know the reason,

Upvotes: 1

Views: 1139

Answers (3)

Chandan
Chandan

Reputation: 772

You can try this:

df2 = pd.DataFrame({ 'A' : 1.,
                   'B' : pd.Timestamp('20130102'),
                  'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
                   'D' : np.array([3] * 4,dtype='int32'),
                   'E' : pd.Series(["TEST","Train","test","train"]),
                    'F' : 'foo' })

mylist = list(df2.select_dtypes(include=['object']).columns)  # in dataframe    
                                                 #string is stored as object

for i in mylist:
   df2[i]= df2[i].str.lower()

Upvotes: 0

Mohammad Yusuf
Mohammad Yusuf

Reputation: 17054

You can use applymap function.

import pandas as pd

df1 = pd.DataFrame({'MovieName': ['LIGHTS OUT', 'Legend'], 'Actors':['MARIA Bello', 'Tom Hard']})
df2=df1.applymap(lambda x: x.lower())
print df1, "\n"
print df2

Output:

        Actors   MovieName
0  MARIA Bello  LIGHTS OUT
1     Tom Hard      Legend 

        Actors   MovieName
0  maria bello  lights out
1     tom hard      legend

Upvotes: 1

linpingta
linpingta

Reputation: 2620

Try to use str.lower on Series object.

Support your DataFrame like below:

df = pd.DataFrame(dict(name=["HERE", "We", "are"]))

   name
0  HERE
1    We
2   are

Then lower all values and output:

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

   name
0  here
1    we
2   are

Upvotes: 0

Related Questions