Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grab all but one element in pandas series

I have a pandas Series that has many values indexed by strings. I want to grab all of them except for one.

my_series.ix['intercept'] #<--- this has the value I don't want

Is there a way to grab everything in my_series except for what is returned by my_series.ix['intercept']?

like image 930
sedavidw Avatar asked Dec 16 '14 21:12

sedavidw


1 Answers

You could construct a mask -- a boolean array which is True where the Series index equals the particular value:

mask = my_series.index.isin(['intercept'])

Then you could select the remaining rows in the typical fashion:

my_series.loc[~mask]

Note that if the value occurs in the index more than once, then all rows with the same index will be removed:

my_series = pd.Series([10,20,30,40], index=['foo','intercept','baz','intercept'])
# foo          10
# intercept    20
# baz          30
# intercept    40
# dtype: int64

mask = my_series.index.isin(['intercept'])
print(my_series.loc[~mask])

yields

foo    10
baz    30
dtype: int64
like image 155
unutbu Avatar answered Sep 16 '22 22:09

unutbu