Python | Pandas Series.str.capitalize()

Series.str.capitalize() used to capitalize the string elements in pandas series. Series is a type of data structure which is used as same as list in python. Series can contain the different types of data as we want to enter in the series like list.

Syntax : Series.str.capitalize()

Parameters: None



Returns: Series/Index of objects

To get the link to csv file, click on nba.csv

Code #1 :
We are using the pandas Series.str.capitalize() method which helps to convert the very first letter of given series in capital and rest all the characters are remain same the a particular string.

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
  
data = pd.read_csv("nba.csv")
  
g = pd.Series(data['Name'].head())
print(g.str.lower(), end ='\n\n')
print(g.str.capitalize())

chevron_right


Output:
As we have explained that only first letter should be capitalize rest of all should be same. As you can see the output given below.

     Before

0    avery bradley
1      jae crowder
2     john holland
3      r.j. hunter
4    jonas jerebko
Name: Name, dtype: object

     After

0    Avery bradley
1      Jae crowder
2     John holland
3      R.j. hunter
4    Jonas jerebko
Name: Name, dtype: object

 
Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
  
data = pd.read_csv("nba.csv")
  
g = pd.Series(data['Team'].head())
print(g.str.lower(), end ='\n\n')
print(g.str.capitalize())

chevron_right


Output:

     Before

0    boston celtics
1    boston celtics
2    boston celtics
3    boston celtics
4    boston celtics
Name: Team, dtype: object
    
     After

0    Boston celtics
1    Boston celtics
2    Boston celtics
3    Boston celtics
4    Boston celtics
Name: Team, dtype: object


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.