擅长:python、mysql、java
<p>不需要学习,你可以用熊猫来学习:</p>
<pre class="lang-py prettyprint-override"><code>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())
</code></pre>
<p>输出为:</p>
<pre><code> 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
</code></pre>