×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Updating values in csv using python

Updating values in csv using python

Updating values in csv using python

(OP)
Thanks in advance for your help.

I am new to python and attempting to update a column, CUST_GENDER, in a csv.

I need to update the CUST_GENDER field by substituting either 1 or 0 depending on Male or Female. Once I have done that, I need it to save and leave the other values in the column unaffected. With the routine I wrote below to update the CUST_GENDER column, the substitution works, but it then wipes out the other vales in that column.

What am I missing after the 'return 1' to stop it deleting the rest of the values and print and save?
_________________________________________________________________
import pandas as pd
import numpy as np
df=pd.read_csv("Marketing Campaign data2.csv")
print(df['CUST_GENDER'].value_counts())

def gender_code(CUST_GENDER):
if CUST_GENDER == 'M':
return 0
if CUST_GENDER == 'F':
return 1

df['CUST_GENDER'] = df['CUST_GENDER'].apply(gender_code)

print(df['CUST_GENDER'].value_counts())

df.to_csv('Marketing Campaign data2.csv', index=False)
_______________________________________________________________

RE: Updating values in csv using python

Hi

Python code without indentation is pretty useless, so please edit your post and enclose the code between [code] and [/code] TGML tags to preserve formatting.

Feherke.
feherke.github.io

RE: Updating values in csv using python

(OP)
Take 2 with [code} tags applied.


Thanks in advance for your help.

I am new to python and attempting to update a column, CUST_GENDER, in a csv.

I need to update the CUST_GENDER field by substituting either 1 or 0 depending on Male or Female. Once I have done that, I need it to save and leave the other values in the column unaffected. With the routine I wrote below to update the CUST_GENDER column, the substitution works, but it then wipes out the other vales in that column.

What am I missing after the 'return 1' to stop it deleting the rest of the values and print and save?


CODE

import pandas as pd
import numpy as np
df=pd.read_csv("Marketing Campaign data2.csv")
print(df['CUST_GENDER'].value_counts())

def gender_code(CUST_GENDER):
    if CUST_GENDER == 'M':
        return 0
    if CUST_GENDER == 'F':
        return 1
    
df['CUST_GENDER'] = df['CUST_GENDER'].apply(gender_code)

print(df['CUST_GENDER'].value_counts())

df.to_csv('Marketing Campaign data2.csv', index=False) 

RE: Updating values in csv using python

Hi

Your code works fine for me with both Python 2 and Python 3 transforming this :

CODE --> Marketing Campaign data2.csv - before

id,CUST_GENDER,blah-blah
1,M,one
2,F,two
3,M,three
4,F,four 
into this :

CODE --> Marketing Campaign data2.csv - after

id,CUST_GENDER,blah-blah
1,0,one
2,1,two
3,0,three
4,1,four 
Could you post a short ( and anonymized, if needed ) sample from your CSV file ?

Feherke.
feherke.github.io

RE: Updating values in csv using python

Hi

Or do you mean not all values in CUST_GENDER column are "M" or "F" ?

CODE --> Python

def gender_code(CUST_GENDER):
    if CUST_GENDER == 'M':
        return 0
    elif CUST_GENDER == 'F':
        return 1
    else:
        return CUST_GENDER 

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close