Python for loop over two lists for all the pairs
<p>I have a dataframe with date, id - I need to pull out each date and id combination and create a new dataframe.</p>

<pre><code>  date           id
2016-05-13       abc
2016-05-13       pqr
2016-05-14       abc
2016-05-14       pqr

ids = list(sorted(set(df['id'])))
Out: ['abc','pqr']

dates = list(sorted(set(df[df.id == ids[i]]['date'])))
Out: ['2016-05-13','2016-05-14']

for i in range(0,len(ids)):
    df2 = df[(df.date == dates[i]) &amp; (df.id == id[i])]
</code></pre>

<p>The above code is resulting the output (<code>df2</code>) for relative index values only (First date, First Id &amp; Second date, Second Id), but I need the output for all the pairs. Please let me know what to change in the loop?</p>

<p>to get all the pairs <code>ids</code> vs. <code>dates</code>, you could use <code>itertools</code> as</p>

<pre><code>import itertools

for iid, ddate in itertools.product(ids, dates):
    df2 = df[(df.date == ddate) &amp; (df.id == iid)]
</code></pre>

python pandas for-loop iteration