Reputation: 641
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
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
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