我有一个非常大的数据框,里面有meterid零售价。当同时存在正值和零值时,我只想输出meterIds。我当前的查询性能非常重,我还无法检查其正确性。
有没有更有表现力的方式?
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))
我的输出应该如下所示:"meterID: x"
4条答案
按热度按时间toiithl61#
zer
o是否屏蔽any
值是否满足它们&
它们;提供ID的真/假序列作为所需结果_
是最后一个赋值的东西,所以我用它引用前面的输出。你可以在115做s = ...
,也可以做s[s].index.tolist()
8yoxcaq72#
这也是可行的:
但Mustafa Aydın's更优雅。
mhd8tkvw3#
下面是使用
group by
的另一种方法,让我们知道哪种方法性能更好:t5fffqht4#
您可以尝试: