Home > OS >  Find groups where a certain condition applies to every value in it
Find groups where a certain condition applies to every value in it

Time:06-25

grouped=exp.groupby(["country","year"])["value"].sum().reset_index().sort_values(["country","year"])
grouped["prev_year"]=grouped.groupby("country")["value"].apply(lambda x:x.shift(1))
grouped["increase_vs_prev_year"]=((100*grouped.value-grouped.prev_year)/grouped.prev_year-100).round(1)
grouped

Here is the output for the code: enter image description here


I want to find countries where increase_vs_prev_year was more than 0 in every year

CodePudding user response:

If need countries with greater like 0 per all values (year)s comapre values for greater by Series.gt and then aggregate GroupBy.all, last filter indices for countries:

s = grouped["increase_vs_prev_year"].gt(0).groupby(grouped["country"]).all()

out = s.index[s].tolist()
  • Related