python - 如何根据第 2 列条件获取第 1 列中的唯一值
问题描述
大家好,以上是我正在处理的数据框。我正在尝试从 Stock 列获取唯一值列表,条件是 Type.1 值不等于“卖出”。
因此,预期的结果是云顶、Epure 和墨卡托。Swiber 不会出现在列表中,因为该记录在 Type.1 列下具有“Sell”值。
我尝试使用此代码,但遇到错误。我可以就如何解决这个问题寻求建议吗?
#Tried this but fail with error ValueError: either both or neither of x and y should be given
companyNames= np.where(portfolioDf["Type.1"] is not "Sell", portfolioDf.loc[:,"Stock"])
#Tried this also fail with error ValueError: either both or neither of x and y should be given
companyNames= np.where(portfolioDf["Type.1"] is not "Sell", portfolioDf.Stock.unique())
解决方案
尝试:
to_remove = df.loc[df["Type.1"] == "Sell", "Stock"]
print(df.loc[~df["Stock"].isin(to_remove), "Stock"].unique())
推荐阅读
- image - 如何用图像填充整个框架?
- python - 如何重构此递归策略表达式 Strategy 以参数化其长度?
- powershell - Powershell将进程的pid添加到FileVersionInfo
- php - How to insert href link in this button tag
- python - Python Regex: AttributeError: 'str' object has no attribute 'Match'
- javascript - 创建递归表单元素
- azure - Kubernetes Service does not map the right port
- php - 为什么 php artisan: schedule run 命令不执行 artisan 命令?
- laravel - 在 Laravel 中为事件定义队列
- javascript - 在模型末尾附加两位小数