Reputation: 103
I am trying to preprocess data.
data = {'Country':['Germany', 'Turkey', 'England', 'Turkey', 'Germany', 'Turkey'],
'Age':['44', '32', '27', '29', '31', '25'],
'Salary':['5400', '8500', '7200', '4800', '6200', '10850'],
'Purchased':['yes', 'yes', 'no', 'yes', 'no', 'yes']}
df = pd.DataFrame(data)
X = df.iloc[:,0].values
Expected result is like this:
|---|---|---|----|-------|---|
| 1 | 0 | 0 | 44 | 5400 | 1 |
| 0 | 1 | 0 | 32 | 8500 | 1 |
| 0 | 0 | 1 | 27 | 7200 | 0 |
| 0 | 1 | 0 | 29 | 4800 | 1 |
| 1 | 0 | 0 | 31 | 6200 | 0 |
| 0 | 1 | 0 | 25 | 10850 | 1 |
Here is the code that failed.
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
ct = ColumnTransformer([("city_category", OneHotEncoder(dtype='int'), [0])], remainder="passthrough")
X = ct.fit_transform(X)
Output:
IndexError: tuple index out of range
I want to learn that how to use ColumnTransformer function in this situations?
Upvotes: 1
Views: 786
Reputation: 2648
X_transformer = ColumnTransformer(
transformers=[
("Country", # Just a name
OneHotEncoder(), # The transformer class
[0] # The column(s) to be applied on.
)
], remainder='passthrough'
)
X = X_transformer.fit_transform(X)
print(X)
Upvotes: 0
Reputation: 5698
No need for sklearn, you can do this with pandas:
import pandas as pd
data = {
"Country": ["Germany", "Turkey", "England", "Turkey", "Germany", "Turkey"],
"Age": ["44", "32", "27", "29", "31", "25"],
"Salary": ["5400", "8500", "7200", "4800", "6200", "10850"],
"Purchased": ["yes", "yes", "no", "yes", "no", "yes"],
}
df = pd.DataFrame(data)
df = pd.concat([pd.get_dummies(df["Country"]), df.drop("Country", axis=1)], axis=1)
df[["Age", "Salary"]] = df[["Age", "Salary"]].astype(int)
df["Purchased"] = df["Purchased"].map(lambda x: x == "yes").astype(int)
print(df.head())
The output is:
England Germany Turkey Age Salary Purchased
0 0 1 0 44 5400 1
1 0 0 1 32 8500 1
2 1 0 0 27 7200 0
3 0 0 1 29 4800 1
4 0 1 0 31 6200 0
Upvotes: 1