Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating values in csv using python 1

Status
Not open for further replies.

Pegasus101

IS-IT--Management
Aug 1, 2014
8
0
0
GB
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)
_______________________________________________________________

 
Hi

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


Feherke.
feherke.github.io
 
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)
 
Hi

Your code works fine for me with both Python 2 and Python 3 transforming this :
Code:
id,CUST_GENDER,blah-blah
1,M,one
2,F,two
3,M,three
4,F,four
into this :
Code:
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
 
Hi

Or do you mean not all values in CUST_GENDER column are "M" or "F" ?
Python:
[b]def[/b] [COLOR=orange]gender_code[/color][teal]([/teal]CUST_GENDER[teal]):[/teal]
    [b]if[/b] CUST_GENDER [teal]==[/teal] [i][green]'M'[/green][/i][teal]:[/teal]
        [b]return[/b] [purple]0[/purple]
    [b]elif[/b] CUST_GENDER [teal]==[/teal] [i][green]'F'[/green][/i][teal]:[/teal]
        [b]return[/b] [purple]1[/purple]
    [b]else[/b][teal]:[/teal]
        [b]return[/b] CUST_GENDER


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top