When I execute the Python 3.4.4 code below to capitalize values under the ident column, I get no errors but my output is not capitalized. Need help figuring out the problem.
import pandas as pd
import numpy as np
#Create a dataframe
data = {'ident': ['Jack', 'Mary', 'Teresa', 'James', 'Anna'],
'year': [2001, 2002, 2003, 2004, 2007],
'reports': [67, 5, 36, 9, 14],
'scope': [17, 102, 57, 49, 77]
}
df = pd.DataFrame(data, index = ['Bahia', 'Pico', 'Santa Fe', 'Maine',
'Zuma'])
#Create a capitalizer function
capitalize = lambda x: x.upper()
#Create a capitalizing function over the column 'ident'
df['ident'].apply(capitalize)
print (df)
Changes to the data frame are not directly saved back. Replace
df['ident'].apply(capitalize)
with
df['ident'] = df['ident'].apply(capitalize)
and it will work.
So the complete code looks like:
import pandas as pd
import numpy as np
#Create a dataframe
data = {'ident': ['Jack', 'Mary', 'Teresa', 'James', 'Anna'],
'year': [2001, 2002, 2003, 2004, 2007],
'reports': [67, 5, 36, 9, 14],
'scope': [17, 102, 57, 49, 77]
}
df = pd.DataFrame(data, index = ['Bahia', 'Pico', 'Santa Fe', 'Maine','Zuma'])
#Create a capitalizer function
capitalize = lambda x: x.upper()
#Create a capitalizing function over the column 'ident'
df['ident'] = df['ident'].apply(capitalize)
print (df)
Or do it even nicer (imho) df.ident=df.ident.str.upper()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With