1

There are 16 categorical variables in my data set.I would like to apply a 0-1 conversion to some of them one-hot.

one hot code : df_one_hot =pd.get_dummies(df,columns = ["guardian"],prefix = ["guardian"]) df_one_hot.head()

0-1 code : df["new_day"] =np.where(df["day"].str.contains("Sun"),1,0)

because I have more than one application, I want all the conversions I do to be permanent.I applied one hot to the first data.when I applied it to the second data, I found that what I applied above was not permanent.how can I make permanent operations for both conversions? inplace=True, this function no

import pandas as pd 
df = pd.read_csv("../data/student-mat.csv", sep=';')
df
.
.
.
df_one_hot =pd.get_dummies(df,columns = ["reason"],prefix = ["reason"])
df_one_hot.head()
#I am doing the first conversion. But when I do it again for the second data, 
#the first conversion is not permanent.
df_one_hot =pd.get_dummies(df,columns = ["guardian"],prefix = ["guardian"])
df_one_hot.head()
  • I think you want some like this question? [adding dummy columns to the original dataframe](https://stackoverflow.com/questions/23208745/adding-dummy-columns-to-the-original-dataframe) – Bernardo stearns reisen Apr 15 '20 at 19:41
  • no I want to apply it to categorical data and my problem is not to apply it to the second data –  Apr 15 '20 at 20:01

1 Answers1

0

Actually, you didn't do it again for the second data, but the first data.

import pandas as pd 
df = pd.read_csv("../data/student-mat.csv", sep=';')

# pass "df" to pd.get_dummies()
df_one_hot = pd.get_dummies(df, columns=["reason"],prefix=["reason"])
df_one_hot.head()

# passed again "df" to pd.get_dummies()
df_one_hot = pd.get_dummies(df, columns=["guardian"], prefix=["guardian"])

## solution => pass the df_one_hot to pd.get_dummies
# df_one_hot = pd.get_dummies(df_one_hot, columns=["guardian"], prefix=["guardian"])
Hsuning
  • 76
  • 2