Python Forum
How to change a dataframe column to lower case
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change a dataframe column to lower case
#1
# 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
Reply
#2
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#3
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
Reply
#4
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#5
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)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding PD DataFrame column bsben 2 245 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 690 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 918 Feb-15-2023, 05:34 PM
Last Post: zsousa
  How to apply function lower() to the list? Toltimtixma 2 756 Feb-10-2023, 05:15 PM
Last Post: Toltimtixma
  Difference one column in a dataframe Scott 0 619 Feb-10-2023, 08:41 AM
Last Post: Scott
  splitting a Dataframe Column in two parts nafshar 2 912 Jan-30-2023, 01:19 PM
Last Post: nafshar
  Change a numpy array to a dataframe Led_Zeppelin 3 1,065 Jan-26-2023, 09:01 PM
Last Post: deanhystad
  Beginner Higher Lower Game wallytan 2 1,539 Sep-29-2022, 05:14 PM
Last Post: deanhystad
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 798 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  renaming the 0 column in a dataframe Led_Zeppelin 5 1,461 Aug-16-2022, 04:07 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020