I\'m running Python 2.7
with the Pandas 0.11.0
library installed.
I\'ve been looking around a haven\'t found an answer to this question, so
Two options come to mind. First, use isin
and a mask:
>>> df
zip x y access
0 123 1 1 4
1 123 1 1 6
2 133 1 2 3
3 145 2 2 3
4 167 3 1 1
5 167 3 1 2
>>> keep = [123, 133]
>>> df_yes = df[df['zip'].isin(keep)]
>>> df_no = df[~df['zip'].isin(keep)]
>>> df_yes
zip x y access
0 123 1 1 4
1 123 1 1 6
2 133 1 2 3
>>> df_no
zip x y access
3 145 2 2 3
4 167 3 1 1
5 167 3 1 2
Second, use groupby
:
>>> grouped = df.groupby(df['zip'].isin(keep))
and then any of
>>> grouped.get_group(True)
zip x y access
0 123 1 1 4
1 123 1 1 6
2 133 1 2 3
>>> grouped.get_group(False)
zip x y access
3 145 2 2 3
4 167 3 1 1
5 167 3 1 2
>>> [g for k,g in list(grouped)]
[ zip x y access
3 145 2 2 3
4 167 3 1 1
5 167 3 1 2, zip x y access
0 123 1 1 4
1 123 1 1 6
2 133 1 2 3]
>>> dict(list(grouped))
{False: zip x y access
3 145 2 2 3
4 167 3 1 1
5 167 3 1 2, True: zip x y access
0 123 1 1 4
1 123 1 1 6
2 133 1 2 3}
>>> dict(list(grouped)).values()
[ zip x y access
3 145 2 2 3
4 167 3 1 1
5 167 3 1 2, zip x y access
0 123 1 1 4
1 123 1 1 6
2 133 1 2 3]
Which makes most sense depends upon the context, but I think you get the idea.