Python Forum

Full Version: How to change a dataframe column to lower case
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [['tom', 10], ['nick', 15], ['juli', 14]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'AGE']) 
I want to change on of the field AGE to age, keep Name unchanged .
How Can I do it?
Thanks,
Jeff
(Oct-29-2019, 03:42 PM)zhujp98 Wrote: [ -> ]
# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [['tom', 10], ['nick', 15], ['juli', 14]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'AGE']) 
I want to change on of the field AGE to age, keep Name unchanged .
How Can I do it?
Thanks,
Jeff

Hi!

If I understand correctly, it is you who is creating the columns and giving the names of the columns with the line:
df = pd.DataFrame(data, columns = ['Name', 'AGE']) 
so have you tried changing that line to:
df = pd.DataFrame(data, columns = ['Name', 'age']) 
All the best,
Thanks,
I want to use program to do that.

The short code is just a simple example. I actually have a very large table with 50 columns. The dataframe is created from a .csv file. I want to change one of columns to lower case.
Thanks,
Jeff
(Oct-29-2019, 04:11 PM)zhujp98 Wrote: [ -> ]I want to change one of columns to lower case.

Hi again!

Then maybe you could use something like:

df['AGE'].str.lower()
and if it is for all the columns, you could use something like:

data.columns = [x.lower() for x in data.columns]
I have based my answer on these:

https://stackoverflow.com/questions/2224...ing-values

https://stackoverflow.com/questions/1972...-lowercase

All the best,
If you are trying to change the name of the column, rather than the values of the column, you can use rename:

df.rename(columns = {'AGE': 'age'}, inplace = True)
thanks!