How to change values ​​in a Python dataframe

I've been looking for an answer for the last 30 minutes, but the only solutions are either for a single column or for R. I have a dataset where I want to change the values ​​('Y / N') 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 as follows. This does not raise an error, but the values ​​in the data frame do not change. I guess I need to do something like df = df_new. But how to do that?

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 that I hope to solve 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

      

+6


source to share


6 answers


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

      

+3


source


I think it is easiest to use on : replace

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

      

+6


source


You can change the values ​​using a function.

Example:.

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

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

      

This is how you map each column of your dataframe.

+1


source


Maybe you can try to 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

      

+1


source


This should do:

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

0


source


All of the above solutions are correct, but you can also do the following:

df["infants"] = df["infants"].replace("Y", 1).replace("N", 0).replace("?", 1)

which now that I read more closely, is very similar to use replace with dict!

0


source







All Articles