首页 > 解决方案 > 如何根据第 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())

标签: pythondataframe

解决方案


尝试:

to_remove = df.loc[df["Type.1"] == "Sell", "Stock"]
print(df.loc[~df["Stock"].isin(to_remove), "Stock"].unique())

推荐阅读