convert columns to uppercase or lowercase in pandas | str.lower(), str.upper() & map in pandas
In this pandas tutorial, we will discuss different ways to:
-
convert columns to uppercase pandas,
-
convert columns to lowercase pandas,
-
str.lower() pandas & str.upper() pandas,
-
map in pandas,
Here we will convert columns to uppercase or lowercase in pandas using str.lower() pandas, str.upper() pandas & map in pandas. But first lets prepare a dataframe on which we will work to show these conversions.
Pandas DataFrame is a two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column. So It will store the data in rows and columns.
We can create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe, The data can be a dictionary that stores list of values with specified key
Example: Create DataFrame
In this example, we will create dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output: DataFrame is created below
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
Now we are going to use above dataframe to convert columns to lowercase pandas and convert columns to uppercase pandas.
Method 1 - Using str.upper() & str.lower()
We can use str.upper() pandas to convert dataframe columns to upper case and str.lower() pandas to convert dataframe columns to lower case.
str.upper() pandas → convert columns to uppercase pandas.
str.lower() pandas → convert columns to lowercase pandas.
This method will take dataframe columns through columns method and then convert to lower/upper case.
Syntax:
dataframe.columns.str.upper()
dataframe.columns.str.lower()
Example: convert column to uppercase and lowercase in pandas
In this example, we will display the dataframe columns by converting the column names into uppercase and then to lowercase.
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# convert columns to upper case
print(data.columns.str.upper())
# convert columns to lower case
print(data.columns.str.lower())
Output: Here first code convert columns to uppercase pandas and then convert columns to lowercase pandas.
Index(['COLLEGE_ID', 'COLLEGE_NAME', 'COLLEGE_ADDRESS', 'TOTAL STAFF'], dtype='object')
Index(['college_id', 'college_name', 'college_address', 'total staff'], dtype='object')
From the above code, all the column names are converted to upper case using str.upper() and into lower case by using str.lower().
Method 2 - Using map in pandas
We can use map in pandas to convert dataframe columns to upper case with str.upper parameter and convert dataframe columns to lower case with str.lower parameter.
Syntax:
dataframe.columns = map(str.lower, dataframe.columns)
map in pandas method will take two parameters.
-
str.upper/str.lower - To convert the dataframe to upper/lower case
-
columns - to take dataframe columns
Example: map pandas example
In this map pandas example, we will display the dataframe columns by converting the column names into uppercase and lowercase.
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# convert columns to upper case
data.columns=map(str.upper, data.columns)
#display the columns
print(data.columns)
# convert columns to lower case
data.columns = map(str.lower, data.columns)
#display the columns
print(data.columns)
Output: Here first code convert columns to uppercase pandas and then convert columns to lowercase pandas.
Index(['COLLEGE_ID', 'COLLEGE_NAME', 'COLLEGE_ADDRESS', 'TOTAL STAFF'], dtype='object')
Index(['college_id', 'college_name', 'college_address', 'total staff'], dtype='object')
From the above code, all the column names are converted to upper case using str.upper() and into lower case by using str.lower() through map() in pandas function.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Mar 19,2022