Search code examples
pythonpandasseries

How to split pandas Series into two based on whether or not the index contains a certain word?


I have pandas series that I want to split into two: one with all the entries of the original series where index contains a certain word and the other with all the remaining entries.

Getting a series of entries which do contain a certain word in their index is easy:

foo_series = original_series.filter(like = "foo")

But how do I get the rest?


Solution

  • You could drop those indices from the original Series:

    foo_series = original_series.filter(like = "foo")
    non_foo_series = original_series.drop(foo_series.index)
    

    Or use boolean indexing:

    m = original_series.index.str.contains('foo')
    foo_series = original_series[m]
    non_foo_series = original_series[~m]
    

    Example:

    # input
    original_series = pd.Series([1, 2, 3, 4], index=['foo1', 'bar1', 'foo2', 'bar2'])
    
    # outputs
    # foo_series
    foo1    1
    foo2    3
    dtype: int64
    
    # non_foo_series
    bar1    2
    bar2    4
    dtype: int64