Wednesday, July 3, 2024
HomeLanguagesPythonPython | Pandas Series.str.capitalize()

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.




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())


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 :




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())


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
Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments