擅长:python、mysql、java
<p>作为<a href="https://stackoverflow.com/users/2137255/john-galt">John Galt's</a>答案的扩展-</p>
<p>对于以下数据帧</p>
<pre><code> customer item1 item2 item3
0 1 apple milk tomato
1 2 water orange potato
2 3 juice mango chips
</code></pre>
<p>如果你想得到一个包含索引值的字典列表,你可以这样做</p>
<pre><code>df.to_dict('index')
</code></pre>
<p>它输出字典,其中父字典的键是索引值。在这种情况下</p>
<pre><code>{0: {'customer': 1, 'item1': 'apple', 'item2': 'milk', 'item3': 'tomato'},
1: {'customer': 2, 'item1': 'water', 'item2': 'orange', 'item3': 'potato'},
2: {'customer': 3, 'item1': 'juice', 'item2': 'mango', 'item3': 'chips'}}
</code></pre>