簡體   English   中英

如果列中的行之一匹配某個值,如何返回 dataframe

[英]how to return a dataframe if one of rows in column match certain value

我有 dataframe,如果其中一個值很高,我希望它返回給我 dataframe

df

a     b    c     d    e    f 
high low  high  low  high low
low  low  low   low  low  high
low  low  low   low  low  low

如果我們在列中過濾它,我知道該怎么做

df[df[a]=="high"]

如果我嘗試循環該過程但它不起作用

for column in df.columns:
    df_high=df[df[column]=="high"]

df_high 返回 0 行。 預期結果:

 a    b   c    d   e    f
high low high low high low
low  low low  low low  high

Use, DataFrame.eq along with DataFrame.any along axis=1 to create a boolean mask then use this mask to filter out the rows in dataframe:

mask = df.eq('high').any(axis=1)
df1 = df[mask]

結果:

# print(df1)

     a    b     c    d     e     f
0  high  low  high  low  high   low
1   low  low   low  low   low  high

我會這樣做

df = pd.DataFrame({'a':['high', 'low', 'low'], 'b':['low', 'high', 'low']})
df.loc[(df=='high').sum(axis=1) > 0]

暫無
暫無

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

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