擅长:python、mysql、java
<p>您可以对堆栈数据集使用<code>get_dummies</code>,然后使用groupby和sum:</p>
<pre><code>pd.get_dummies(df.set_index('Owner').stack()).groupby('Owner').sum()
Cat Dog Ferret Hamster Mouse Rat
Owner
Bob 0 1 0 0 0 0
John 1 0 0 0 1 0
Lee 1 1 0 0 0 0
Jane 0 0 1 1 0 1
</code></pre>