Capitalize() Function in python is used to capitalize the First character of the string or first character of the column in dataframe. Let’s see an example for both.
capitalize() function in python for a string
# Capitalize Function for string in python str = "this is beautiful earth!!"; str.capitalize()
So the output will be
‘This is beautiful earth!!’
capitalize() function in python for a column in a dataframe – pandas:
Create dataframe:
#create dataframe import pandas as pd d = {'Quarters' : ['quarter1','quarter2','quarter3','quarter4'], 'Revenue':[23400344.567,54363744.678,56789117.456,4132454.987]} df=pd.DataFrame(d) print df
Resultant dataframe will be
Now we will capitalize the every elements of column Quarters with map function as shown below
# capitalize the column Quarters of a dataframe df['Quarters'] = map(lambda x: x.capitalize(), df['Quarters']) print df
Only the first letter of the string will be capitalized, so the output will be