擅长:python、mysql、java
<pre><code>before['has_car'] = True
Out[93]:
car Email Person has_car
Ford john@example.com John True
Toyota mary@example.com Mary True
Nissan jane@example.com Jane True
Nissan john@example.com John True
Ford mary@example.com Mary True
df = before.pivot_table(index = ['Person' , 'Email'], columns= 'Car' , values='has_car')
Out[89]:
Ford Nissan Toyota
Person Email
Jane jane@example.com NaN True NaN
John john@example.com True True NaN
Mary mary@example.com True NaN True
df.fillna(False).reset_index()
Out[102]:
Car Person Email Ford Nissan Toyota
0 Jane jane@example.com False True False
1 John john@example.com True True False
2 Mary mary@example.com True False True
</code></pre>