String capitalize() Function in python – Capitalize first character of column in dataframe pandas

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

Capitalize Function in python pandas 1

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

Capitalize Function in python pandas 2

previous capitalize() Function in python pandas                                                                                                           next capitalize() Function in python pandas

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.