I have a very large dataframe with meterId retailPrice. I want to output only the meterIds when there are both positive and 0 values. My current query is very performance heavy and I could not check the correctness yet.
Is there a more performant way?
d = {'meterId': ["x", "x", "y", "y", "z", "z"], 'retailPrice': [1, 0, 0, 0, 1, 1]}
df = pd.DataFrame(data=d)
df
tmp = pd.DataFrame(df["meterId"])
for x in tmp["meterId"]:`
zero_values = pd.DataFrame(df.loc[(df['meterId'] == x) & df['retailPrice'] == 0)])
positive_values = pd.DataFrame(df.loc[(df['meterId'] == x) & (df['retailPrice'] > 0)])
if not zero_values.empty and not positive_values.empty:
print("meterID: " + str(x))
My output should be like this:
"meterID: x"
