Python Pandas - Removing Rows From A DataFrame Based on a Previously Obtained Subset

后端 未结 1 829
借酒劲吻你
借酒劲吻你 2020-12-16 00:22

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

相关标签:
1条回答
  • 2020-12-16 01:08

    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.

    0 讨论(0)
提交回复
热议问题