Apply uppercase letters to column in pandas dataframe

Python Methods and Functions

A situation may arise when we need to write each letter in any specific column in a given data frame. Let's see how we can capitalize a column in a Pandas dataframe.

Let's create nba.csv using nba.csv .

# Import pandas package

import pandas as pd 

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering / wp-content / uploads / nba.csv "

  
# call the head () method
# save new variable

data_top = data .head ( 10

 
# display
data_top

Output:

There are certain methods that we can change / change in the case of a column in a Pandas dataframe. Let's see how we can capitalize a column in a Pandas dataframe using the upper () method.

Method # 1:

# Import pandas package

import pandas as pd 

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering / wp-content / uploads / nba.csv "

  
# display

data [ ' Name' ] = data [ 'Name' ]. str .upper ()

 
data.head ()

Output:

Method # 2: Using lambda with upper ()

# Import pandas package

import pandas as pd 

  
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

 

 
# remove null values ​​to avoid errors

data.dropna (inplace = True

 
# Applying the upper () method to the column & # 39; College & # 39;

data [ 'College' ]. apply ( lambda x: x.upper ()). head ( 10 )

Output:





Get Solution for free from DataCamp guru