簡體   English   中英

where() function 系列與 DataFrame 需要解釋

[英]where() function explanation needed on Series Vs DataFrame

df 有 A、B、C、D、E 列,假設“A”列是字符串,rest 是數字。

df["A"].where(df[B] > 100).dropna()返回列“A”,只要“B”的值 > 100

我的問題是df["A"] (它是原始df的視圖)沒有“B”列,那么“where”子句如何應用於B列。[where()子句應用於df["A"]但不是整個 "df"]

df["A"]的類型是 Pandas 系列,即使如此,“B”列上的where()子句也有點令人困惑如何應用它。

很容易得到 dataframe 的所有列,而不僅僅是A

只需刪除["A"]部分:

df["A"].where(df["B"] > 100).dropna()

df.where(df["B"] > 100).dropna()

現在您可以執行以下操作:

>>> subset = df.where(df["B"] > 100).dropna()
>>> subset["B"]
...
>>> subset["A"]

注意:不是使用where + dropna ,而是以下更短的等效解決方案。

代替

df.where(df["B"] > 100).dropna()

只需使用

df[df["B"] > 100]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM