Pandas DataFrame slicing based on logical conditions?
<p>I have this dataframe called data:</p>

<pre><code>       Subjects  Professor  StudentID
8     Chemistry       Jane        999
1     Chemistry       Jane       3455
0     Chemistry     Joseph       1234
2       History       Jane       3455
6       History      Smith        323
7       History      Smith        999
3   Mathematics        Doe      56767
10  Mathematics   Einstein       3455
5       Physics   Einstein       2834
9       Physics      Smith        323
4       Physics      Smith        999
</code></pre>

<p>I want to run this query "Professors with at least 2 classes with 2 or more of the same students". Desired Output</p>

<pre><code>Smith: Physics, History, 323, 999
</code></pre>

<p>I am familiar with SQL and could have done this easily, but I am still beginner in Python. How to achieve this output in Python? Another line of thought is to convert this dataframe into SQL database and have a SQL interface through python to run queries. Is there a way to accomplish that?</p>

<pre><code>students_and_subjects = df.groupby(
                               ['Professor', 'Subjects']
                           ).StudentID.nunique().ge(2) \
                          .groupby(level='Professor').sum().ge(2)

df[df.Professor.map(students_and_subjects)]
</code></pre>

<p><a href="http://i.stack.imgur.com/NBlPy.png" rel="nofollow"><img src="http://i.stack.imgur.com/NBlPy.png" alt="enter image description here"></a></p>

python mysql pandas