I have a python pandas dataframe with columns “location”, “product” and “amount.” Example:
data = [['park', 'cookie', 2], ['park', 'muffin', 0], ['park', 'scone', 3], ['mall', 'cookie', 0], ['mall', 'muffin', 0], ['mall', 'scone', 0], ['store', 'cookie', 8]]
df = pd.DataFrame(data, columns=['location', 'product', 'amount'])
I want to figure out if all amounts at a certain location is 0. To do this I take two conditions on the dataframe and then check if the dataframe is empty like this:
def location_has_nothing(loc):
location_condition = df['location'] == loc
amount_condition = df['amount'] != 0
df_filtered = df[location_condition][amount_condition]
return df_filtered.empty
So if loc = park
, it would return false and if loc = mall
it would be true. I’m new to data frames so I am wondering if anyone knows a better way to do this or if this is how you would do it?
Thank you!