Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change values in a dataframe Python

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
like image 420
handavidbang Avatar asked Jul 13 '17 03:07

handavidbang


People also ask

How do you replace a value in a DataFrame column in Python?

DataFrame. replace() function is used to replace values in column (one value with another value on all columns). This method takes to_replace, value, inplace, limit, regex and method as parameters and returns a new DataFrame. When inplace=True is used, it replaces on existing DataFrame object and returns None value.

Can we modify a data inside a DataFrame in Python?

3. Python replace() method to update values in a dataframe. Using Python replace() method, we can update or change the value of any string within a data frame. We need not provide the index or label values to it.


1 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
like image 192
Luis Miguel Avatar answered Sep 28 '22 06:09

Luis Miguel