Friday, December 27, 2024
Google search engine
HomeLanguagesPython | 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
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments