Graphviz: write result to file
<p>I have dataframe</p>

<pre><code>ID  domain  search_term
111 vk.com  вконтакте
111 twitter.com фэйсбук
111 facebook.com    твиттер
222 avito.ru    купить машину
222 vk.com  вконтакте
333 twitter.com твиттер
333 apple.com   купить айфон
333 rbk.ru  новости
</code></pre>

<p>I try to create chain with nodes and write it to file. I use</p>

<pre><code>domains = df['domain'].values.tolist()
search_terms = df['search_term'].values.tolist()
ids = df['ID'].values.tolist()
f = Digraph('finite_state_machine', filename='fsm.gv', encoding='utf-8')
f.body.extend(['rankdir=LR', 'size="5,5"'])
f.attr('node', shape='circle')
for i, (id, domain, search_term) in enumerate(zip(ids, domains, search_terms)):
    if ids[i] == ids[i - 1]:
        f.edge(domains[i - 1], domains[i], label=search_terms[i])
f.view()
</code></pre>

<p>It returns <a href="https://i.stack.imgur.com/YbhSq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YbhSq.png" alt="this file"></a>
But I want to save it to file, like number of <code>ID</code>. I need to get file <code>111, 222, 333</code>.
I try</p>

<pre><code>for i, (id, domain, search_term) in enumerate(zip(ids, domains, search_terms)):
if ids[i] == ids[i - 1]:
    f = Digraph('finite_state_machine', filename='fsm.gv', encoding='utf-8')
    f.body.extend(['rankdir=LR', 'size="5,5"'])
    f.attr('node', shape='circle')
    f.edge(domains[i - 1], domains[i], label=search_terms[i])
    f.render(filename=str(id))
</code></pre>

<p>But It works wrong. It should return to <code>111</code> and <code>333</code> chain with 3 nodes, but in file I get chains with 2 nodes to <code>111</code> and <code>333</code>. This file to <code>111</code>:
<a href="https://i.stack.imgur.com/Mb431.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Mb431.png" alt="result"></a>
What I do wrong and how can I fix that?</p>