Is there a clean way to filter Pandas Series using a custom function that takes as input both index and value?
Here is a piece of code that achieves what I want to do:
series = pd.Series({"id5":88, "id3":40})
def custom(k,v):
if k=="id5":
return v>20
else:
return v>50
filtered_indexes = []
filtered_values = []
for k,v in series.iteritems():
if custom(k,v):
filtered_indexes.append(k)
filtered_values.append(v)
filtered_series = pd.Series(data=filtered_values, index=filtered_indexes)
My question is: can the same be achieved cleaner and/or more efficiently with syntax like
series.filter(lambda x: custom(x.index, x.value))