Table of Contents
To change column names to lowercase in Pandas, follow these steps:
1. Import the Pandas library into your code.
2. Load your data into a Pandas DataFrame.
3. Use the “columns” attribute to access the list of column names.
4. Use the “str.lower()” method to convert the column names to lowercase.
5. Assign the updated column names back to the “columns” attribute.
6. Verify the changes by printing the column names.
Following these steps will allow you to easily convert column names to lowercase in your Pandas DataFrame. This can be useful for data consistency and easier data manipulation.
Pandas: Change Column Names to Lowercase
You can use the following syntax to change the column names in a pandas DataFrame to lowercase:
df.columns = df.columns.str.lower()
The following example shows how to use this syntax in practice.
Example: Change Column Names to Lowercase in Pandas
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'Team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], 'Points': [18, 22, 19, 14, 14, 11, 20, 28], 'ASSISTS': [5, 7, 7, 9, 12, 9, 9, 4], 'Rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame print(df)
Notice that none of the column names are currently lowercase.
We can use the following syntax to change all column names to lowercase:
#convert all column names to lowercase df.columns = df.columns.str.lower() #view updated DataFrame print(df) team points assists rebounds 0 A 18 5 11 1 B 22 7 8 2 C 19 7 10 3 D 14 9 6 4 E 14 12 6 5 F 11 9 5 6 G 20 9 9 7 H 28 4 12
Notice that all four column names have now been converted to lowercase.
Note that we can also use the following syntax to list out all of the column names without displaying any of the column values:
#display all column names
list(df)
['team', 'points', 'assists', 'rebounds']
We can see that each of the column names has been converted to lowercase.
The following tutorials explain how to perform other common functions with columns of a pandas DataFrame:
Cite this article
stats writer (2024). How to change column names to lowercase in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-change-column-names-to-lowercase-in-pandas/
stats writer. "How to change column names to lowercase in Pandas?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-to-change-column-names-to-lowercase-in-pandas/.
stats writer. "How to change column names to lowercase in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-to-change-column-names-to-lowercase-in-pandas/.
stats writer (2024) 'How to change column names to lowercase in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-change-column-names-to-lowercase-in-pandas/.
[1] stats writer, "How to change column names to lowercase in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How to change column names to lowercase in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
