1

I have a dataset in which I want to change the ('yes/No') values to 1 and 0 respectively in Functioning Day column in following data frame. I feel like copying and pasting a code for times is very inefficient.

my data frame:

Season  Holiday     Functioning Day
Winter  No Holiday  Yes
Winter  No Holiday  Yes
Winter  No Holiday  Yes
Winter  Holiday     Yes
Winter  No Holiday  Yes
Winter  No Holiday  Yes
Winter  No Holiday  Yes
Winter  No Holiday  Yes
Winter  No Holiday  Yes
Winter  No Holiday  No
Winter  No Holiday  Yes
Winter  No Holiday  Yes
Winter  No Holiday  Yes
Winter  No Holiday  Yes
Winter  No Holiday  Yes
Winter  No Holiday  Yes
Winter  No Holiday  Yes

code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sn

data=pd.read_csv('F:/ML_991_Final/Dataset/4/SeoulBikeData.csv')
data.rename(index=data.Date,inplace=True)
data.drop('Date',axis=1,inplace=True)

for i in data.columns: # this part of my code replace yes with 1 
    data.loc[data[i]=='yes',i ] = 1
print(data)

corrMatrix = data.corr()
sn.heatmap(corrMatrix, annot=True)
plt.show()

error:

"F:\python code\venv\Scripts\python.exe" "F:/python code/venv/41.py"
Traceback (most recent call last):
  File "F:\python code\venv\41.py", line 10, in <module>
    for i in data.columns():
TypeError: 'Index' object is not callable

3 Answers 3

2

Use numpy.where() as follows:

data["Functioning Day"] = np.where(data["Functioning Day"] == "yes", 1, 0)

and the problem is solved(:

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the replace function.

data["Functioning Day"] = data["Functioning Day"].replace({"No": 0, "Yes": 1})

Comments

1

OK, adding another option to the answers: You can also use Series.map() to assign the new values.

Used for substituting each value in a Series with another value, that may be derived from a function, a dict or a Series.

df['Functioning Day'] = df['Functioning Day'].map({'Yes': 1, 'No': 0})

If you plan to do a similar replacement for the 'Holiday' column, then you can do both columns at the same time and return the full dataframe using DataFrame.replace():

df = df.replace({'Holiday': {'Holiday': 1, 'No Holiday': 0},
                 'Functioning Day': {'Yes': 1, 'No': 0},
                })

This way of calling df.replace allows you to specify the column names for each replacement mapping that should be done; so can do multiple columns.

Output:

    Season  Holiday  Functioning Day
0   Winter        0                1
1   Winter        0                1
2   Winter        0                1
3   Winter        1                1
4   Winter        0                1
5   Winter        0                1
6   Winter        0                1
7   Winter        0                1
8   Winter        0                1
9   Winter        0                0
10  Winter        0                1
11  Winter        0                1
12  Winter        0                1
13  Winter        0                1
14  Winter        0                1
15  Winter        0                1
16  Winter        0                1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.