Pegasus101
IS-IT--Management
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)
_______________________________________________________________
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)
_______________________________________________________________