How to change values in a dataframe Python

101,807

Solution 1

This should work:

for col in df.columns():
   df.loc[df[col] == 'n', col] = 0
   df.loc[df[col] == 'y', col] = 1
   df.loc[df[col] == '?', col] = 1

Solution 2

I think simpliest is use replace by dict:

np.random.seed(100)
df = pd.DataFrame(np.random.choice(['n','y','?'], size=(5,5)), 
                                   columns=list('ABCDE'))
print (df)
   A  B  C  D  E
0  n  n  n  ?  ?
1  n  ?  y  ?  ?
2  ?  ?  y  n  n
3  n  n  ?  n  y
4  y  ?  ?  n  n

d = {'n':0,'y':1,'?':1}
df = df.replace(d)
print (df)
   A  B  C  D  E
0  0  0  0  1  1
1  0  1  1  1  1
2  1  1  1  0  0
3  0  0  1  0  1
4  1  1  1  0  0

Solution 3

This should do:

df.infants = df.infants.map({ 'Y' : 1, 'N' : 0})

Solution 4

Maybe you can try apply,

import pandas as pd
# create dataframe
number = [1,2,3,4,5]
sex = ['male','female','female','female','male']
df_new = pd.DataFrame()
df_new['number'] = number
df_new['sex'] = sex
df_new.head()
# create def for category to number 0/1
def tran_cat_to_num(df):
    if df['sex'] == 'male':
        return 1
    elif df['sex'] == 'female':
        return 0
# create sex_new 
df_new['sex_new']=df_new.apply(tran_cat_to_num,axis=1)
df_new

raw

   number     sex
0       1    male
1       2  female
2       3  female
3       4  female
4       5    male

after use apply

   number     sex  sex_new
0       1    male        1
1       2  female        0
2       3  female        0
3       4  female        0
4       5    male        1

Solution 5

You can change the values using the map function.

Ex.:

x = {'y': 1, 'n': 0}

for col in df.columns():
    df[col] = df[col].map(x)

This way you map each column of your dataframe.

Share:
101,807
handavidbang
Author by

handavidbang

Updated on July 21, 2022

Comments

  • handavidbang
    handavidbang almost 2 years

    I've searched for an answer for the past 30 min, but the only solutions are either for a single column or in R. I have a dataset in which I want to change the ('Y/N') values to 1 and 0 respectively. I feel like copying and pasting the code below 17 times is very inefficient.

    df.loc[df.infants == 'n', 'infants'] = 0
    df.loc[df.infants == 'y', 'infants'] = 1
    df.loc[df.infants == '?', 'infants'] = 1
    

    My solution is the following. This doesn't cause an error, but the values in the dataframe doesn't change. I'm assuming I need to do something like df = df_new. But how to do this?

    for coln in df:
    for value in coln: 
            if value == 'y':
                value = '1'
            elif value == 'n':
                value = '0'
            else: 
                value = '1'
    

    EDIT: There are 17 columns in this dataset, but there is another dataset I'm hoping to tackle which contains 56 columns.

    republican  n   y   n.1 y.1 y.2 y.3 n.2 n.3 n.4 y.4 ?   y.5 y.6 y.7 n.5 y.8
    0   republican  n   y   n   y   y   y   n   n   n   n   n   y   y   y   n   ?
    1   democrat    ?   y   y   ?   y   y   n   n   n   n   y   n   y   y   n   n
    2   democrat    n   y   y   n   ?   y   n   n   n   n   y   n   y   n   n   y
    3   democrat    y   y   y   n   y   y   n   n   n   n   y   ?   y   y   y   y
    4   democrat    n   y   y   n   y   y   n   n   n   n   n   n   y   y   y   y